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
A common pattern in our current polyfill code is to reassign variables and parameters. How should we work around TS's limitation that mutating a variable doesn't change its type from TS's perspective?
functiontoString(a: unknown){a=String(a);returna;// expected return type: String// actual return type: unknown}toString(42).includes('4')// ^^ Object is of type 'unknown'.(2571)// explicitly declaring a return type doesn't work eitherfunctiontoString2(a: unknown): string{a=String(a);returna;// ^^ Type 'unknown' is not assignable to type 'string'.(2322)}
That being said, this isn't great given that it slightly bloats code-size and runtime as those extra checks have to be included. Internal to Google, we have a system like this that removes these checks in production builds, and we could maybe replicate small parts of it here (using DEBUG) .
Alternatively, could we simply avoid this pattern and rewrite to new variables? Are we really saving that much memory by mutation parameter variables?
A common pattern in our current polyfill code is to reassign variables and parameters. How should we work around TS's limitation that mutating a variable doesn't change its type from TS's perspective?
For example: (TS playground)
There is apparently a future TS proposal microsoft/TypeScript#45870 which may fix this. I commented there to ask if it will help us.
cc @12wrigja
The text was updated successfully, but these errors were encountered: