Typescript: typeguard stop applying for readonly properties of const variables in new closures

Created on 9 Jun 2018  路  6Comments  路  Source: microsoft/TypeScript

See self-explanatory example

Like happens with const variable typeguards that keep applying inside new function-like bodies, I would expect the same for const object literal's readonly properties

Working as Intended

Most helpful comment

Just because you have a readonly reference to an object doesn't mean someone else doesn't have a mutable reference to it

All 6 comments

TypeScript resets narrowing within the scope of a function because it cannot be sure that the outer scope checks are valid when the resulting function is invoked.

See #11498

Correct me if i'm wrong, but the combine use of readonly and const should guarantee that root.foo2 is not undefined. #11498 addresses the case when the root variable is not constof the property is not readonly.

Just because you have a readonly reference to an object doesn't mean someone else doesn't have a mutable reference to it

Ah, I didn't think of that. Thanks for the explanation.

aja, got it thanks! Should I close this issue ?

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fdecampredon picture fdecampredon  路  358Comments

OliverJAsh picture OliverJAsh  路  242Comments

blakeembrey picture blakeembrey  路  171Comments

jonathandturner picture jonathandturner  路  147Comments

Taytay picture Taytay  路  174Comments