-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
mapping over an array of tuple types does not give the correct type #11312
Comments
Tuples are never inferred. On the return, you have to be explicit: let arr: Array<[number, number]> = [[1, 2]]
let mappedArr: Array<[number, number]> = arr.map(([x, y]) => {
return <[number, number]> [x, y];
}); ... or ... let arr: Array<[number, number]> = [[1, 2]]
let mappedArr: Array<[number, number]> = arr.map(([x, y]): [number, number] => {
return [x, y];
}); But of course then you don't have to be explicit about the left hand side then: let arr: Array<[number, number]> = [[1, 2]]
let mappedArr = arr.map(([x, y]): [number, number] => {
return [x, y];
}); |
You can also manually augment |
Which means this is dupe of #6574, I had forgotten. |
I don't think this is a dupe; that issue is about mapping tuples to tuples (and also, it's fixed). This is about mapping arrays of tuples to arrays of tuples. The issue could be more succinctly stated as: "Tuple return types are not inferred". Here's a simple repro case: (note that the return type is (number | string)[] instead of [number, string]) The fact that arrow functions therefore can't map tuples to tuples is just a consequence of that. |
This is another manifestation of #11152.
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; so you see The piece that is missed here, is we know that the |
TypeScript Version: 2.0.3
Code
The
map
callback in the code below is the identity function.Expected behavior:
mappedArr
is of typeArray<[number, number]>
. Compilation works.Actual behavior: Doesn't work. Error message:
TypeScript playground link
The text was updated successfully, but these errors were encountered: