-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mapped array/tuple types not always working as expected #43129
Comments
It seems like you figured this out correctly. What part of this is supposed to be the defect? |
@RyanCavanaugh @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? |
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]>
} |
Is 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 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: |
@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
number
keys only in order(unlike #26063) to get rid of the error:Playground link with relevant code
But notice that
test
still 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
Promise
instead ofInstanceType
, the first issues goes away, althoughnumber
is 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.
The text was updated successfully, but these errors were encountered: