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
Because of the flexibility of javascript, typescript can only safely infer object keys as strings, although in most cases a more precise type is what makes sense. Currently there are several workarounds, but all of the ways I can think of are too verbose and emit unnecessary runtime code.
I propose to introduce a syntax to restrict the type of the for..in variable:
for(letiteratorasKeyTypeiniterable)
The as underlined the fact that it is an unsafe cast, but the syntax is compact.
In both case, the code is rather verbose and arguably a bit technical for the second one, considering this is a rather common use-case. Also some unnecessary js code will be emitted, although it probably has negligible performance impact, I still find it would be cleaner to address this at the typescript level.
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
for..in iterator typing cast coertion
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Because of the flexibility of javascript, typescript can only safely infer object keys as strings, although in most cases a more precise type is what makes sense. Currently there are several workarounds, but all of the ways I can think of are too verbose and emit unnecessary runtime code.
I propose to introduce a syntax to restrict the type of the for..in variable:
The
as
underlined the fact that it is an unsafe cast, but the syntax is compact.π Motivating Example
Consider this example using this feature:
π» Use Cases
Currently, the example above can be implemented using a duplicate variable:
Or the iterator may be coerced to a narrower type using an assertion:
In both case, the code is rather verbose and arguably a bit technical for the second one, considering this is a rather common use-case. Also some unnecessary js code will be emitted, although it probably has negligible performance impact, I still find it would be cleaner to address this at the typescript level.
The text was updated successfully, but these errors were encountered: