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
This is working as intended. The property names of a tuple really are strings. For example, Object.keys([10, 20, 30]) yields ['0', '1', '2'] at run-time. Indexing with numbers really is equivalent to converting the numbers to their string representation. That said, with #23592 we allow you to declare types with numeric property names (which is then reflected as numeric literals in keyof), but it would be a breaking change to switch tuple types to use those. Not to mention the issue that numeric property names would be "swallowed" by the numeric index signature every tuple inherits from Array<T>.
@ahejlsberg Is there a recommended way of "unboxing" the types inside a tuple, without converting the tuple into an object type? Hopefully, there's a workaround that works in v2.8+.
TypeScript Version: 3.2.0-dev.20181023
Search Terms: mapped tuple index string not number
Code
Expected behavior: No error,
typeof Test
is[0, 1, 2]
.Actual behavior:
error TS2322: Type 'number' is not assignable to type '"0"'
/'"1"'
/'"2"'
,typeof Test
is["0", "1", "2"]
.Playground Link: https://www.typescriptlang.org/play/index.html#src=type%20MapToIndexes%3CT%20extends%20unknown%5B%5D%3E%20%3D%20%7B%20%5BP%20in%20keyof%20T%5D%3A%20P%20%7D%3B%0D%0A%0D%0Aconst%20Test%3A%20MapToIndexes%3C%5Bany%2C%20any%2C%20any%5D%3E%20%3D%20%5B0%2C%201%2C%202%5D%3B
Related Issues: encountered while trying to work around #27995
Seems like for mapped tuples the pre-2.9 behaviour of all types being strings is still being applied, so for a case like
UnBoxTuple<[Box<string>, Box<number>]>
would equal[never, never]
instead of the desired[string, number]
.The text was updated successfully, but these errors were encountered: