Repository navigation
Mapped array/tuple types not always working as expected #43129
Description
Activity
- changed the title
[-]Mapped array/tuple types not always working[/-][+]Mapped array/tuple types not always working as expected[/+]on Mar 7, 2021 RyanCavanaugh commented
on Mar 8, 2021 MemberMore actionsIt seems like you figured this out correctly. What part of this is supposed to be the defect?
- addedNeeds More InfoThe issue still hasn't been fully clarifiedThe issue still hasn't been fully clarified
on Mar 8, 2021 Ryan Cavanaugh (@RyanCavanaugh) Joe Pea (@trusktr) Hello guys. Just ran into this. From what I’m able to say, mapping tuples doesn’t seem to work as in #26063
Minimal reproduction playground
What am I missing?
RyanCavanaugh commented
on Jul 7, 2021 MemberMore actionskeyof MyTupleincludes things like"indexOf"because tuples are arrays. This is the expected behavior.You can write this instead:
type Box<T extends string> = { value: T } type MyTuple = [string, ...string[]] type A = { [K in (keyof MyTuple) & number]: Box<MyTuple[K]> }
Reacted by Jiří BrabecIs this the same issue that is catching me here?
type SomeAbstractedType = string; type SpecialObject<T extends SomeAbstractedType> = { foo: T } type SpecialObjectsOf<Ts extends SomeAbstractedType[]> = { [K in keyof Ts]: SpecialObject<Ts[K]>; // ^^^^^ // Type 'Ts[K]' does not satisfy the constraint 'string'. // Type 'Ts[keyof Ts]' is not assignable to type 'string'. // Type 'Ts[string] | Ts[number] | Ts[symbol]' is not assignable to type 'string'. // Type 'Ts[string]' is not assignable to type 'string'.(2344) };
If so, what it boils down to, seems to be this: I don't think we can do type constraints on mapped tuples...due to (imo) unintuitive behavior of
keyof MyTupleTypenot necessarily beingnumber. I'm not sure when I'd ever wantkeyof MyTupleTypeto include'lengthor'indexOf'- perhaps if I want that I would putkeyof Array.prototypeor something.In the playground, I'm able to get values of the mapped type and they're exactly what I want, but the definition of them still errors out:
RyanCavanaugh commented
on May 26, 2022 MemberMore actionsDerek Wiers (@dtwiers) looks like that's been fixed; there's no error in TypeScript 4.7
Closing since there doesn't seem to be anything actionable left

Bug Report
🔎 Search Terms
Mapped array types
🕗 Version & Regression Information
I noticed now in TS 4.1.2.
⏯ Code and Playground Links
There are two problems.
The following has an error in the mapped type despite that I did it just like in #26063 (as far as I know):
Playground link with relevant code
But notice that when you hover on
test, it is a union of all the values of the array including values from non-numeric keys, which is unlike the examples in #26063.There seems to some sort of special casing happening.
I had to specify that I want
numberkeys only in order(unlike #26063) to get rid of the error:Playground link with relevant code
But notice that
teststill contains all the values of all keys, not just array values.Finally, I had to specify numeric keys for
ArrayValues:Playground link with relevant code
But notice if I use
Promiseinstead ofInstanceType, the first issues goes away, althoughnumberis still needed inArrayValues:Playground link with relevant code
🙁 Actual behavior
Does not work like #26063
🙂 Expected behavior
Works like #26063
There seems to be some sort of special casing going on.