You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Object.freeze after object creation doesn't error #32253
Typescript has no way to model the fact that a simple method call changes the type of its parameter.
The only way to model such changes are custom type guards, and these have to be used in some sort of conditional to work:
functionfreezeAndGuard<T>(p: T): p is Readonly<T>{Object.freeze(p);returntrue;}(function(){constb={b: 0};if(!freezeAndGuard(b))return;b.b=1;// Errconstc={c: {d: 0}};if(!freezeAndGuard(c.c))return;c.c.d=1;// Err})();
Typescript has no way to model the fact that a simple method call changes the type of its parameter.
Would also be interested: is this a design limitation of TypeScript? Is there a possibility of changing it? It feels like it would not be a small change...
There were several proposals to implement something like custom type assertion ( #10421, #31512, #31879, #17760, #32130 just to name a few).
It feels small but it actually has big performance implications. From previous comment:
Typescript builds a graph of nodes that can impact the type of variables. This graph includes nodes of constructs that can impact the type of a variable (such as if, switch statemenets, assignments etc). With this change ALL calls would have to be included in this graph (the graph is constructed when type information about the function is not yet available, so there is no way to know that assertString has an impact on CF while other functions don't).
Activity
karlhorky commentedon Jul 4, 2019
If this is an intentional design decision, it would be a good candidate for documentation on microsoft/TypeScript-Handbook#1059
dragomirtitian commentedon Jul 4, 2019
Typescript has no way to model the fact that a simple method call changes the type of its parameter.
The only way to model such changes are custom type guards, and these have to be used in some sort of conditional to work:
Playground link
karlhorky commentedon Jul 4, 2019
Thanks for the answer! I suppose we can tag this as "Working as Intended" or similar and close it then?
In any case, good to have this documented at least here.
Maybe also good to add to the handbook, maybe one or multiple of the following:
Object.freezesection on the Utility Types pageObject.freezekarlhorky commentedon Jul 4, 2019
Would also be interested: is this a design limitation of TypeScript? Is there a possibility of changing it? It feels like it would not be a small change...
dragomirtitian commentedon Jul 4, 2019
There were several proposals to implement something like custom type assertion ( #10421, #31512, #31879, #17760, #32130 just to name a few).
It feels small but it actually has big performance implications. From previous comment:
karlhorky commentedon Jul 5, 2019
Thanks for the detailed explanation! Really helpful to have this all linked together 🙌
karlhorky commentedon Jul 5, 2019
I actually meant to say "not a small change", but I think the
notgot lost in the text. I've emphasized it.jimmywarting commentedon Nov 26, 2021
currently wish i could refactor this:
into this:
IanBellomy commentedon Feb 7, 2025
Curious — rather than a change in type of the parameter, should there not be, ideally, an error when
Object.freezeis called?