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
{{ message }}
Repository navigation
mapping over an array of tuple types does not give the correct type #11312
Expected behavior:mappedArr is of type Array<[number, number]>. Compilation works.
Actual behavior: Doesn't work. Error message:
Type 'number[][]' is not assignable to type '[number, number][]'.
Type 'number[]' is not assignable to type '[number, number]'.
Property '0' is missing in type 'number[]'.
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:
so you see U is only inferred from the return type of the callback function. since tuple types are never inferred unless there is a contextual type (which in this case there is none), a regular array type is inferred for U, then the assignment check is done, and it fails.
The piece that is missed here, is we know that the U[] will be assigned to mappedArr, and we should use this as a contextual type. #11152 tracks propagating using the return type of the contextual signature if one exists as another location for inference.
Activity
kitsonk commentedon Oct 3, 2016
Tuples are never inferred. On the return, you have to be explicit:
... or ...
But of course then you don't have to be explicit about the left hand side then:
gcnew commentedon Oct 3, 2016
You can also manually augment
mapas proposed in #11252.kitsonk commentedon Oct 3, 2016
Which means this is dupe of #6574, I had forgotten.
calebegg commentedon Oct 28, 2016
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:
http://www.typescriptlang.org/play/#src=function%20foo(x%3A%20number%2C%20y%3A%20string)%20%7B%0D%0A%20%20%20%20return%20%5Bx%2C%20y%5D%3B%0D%0A%7D
(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.
mhegazy commentedon Oct 29, 2016
This is another manifestation of #11152.
mapis declared as:so you see
Uis only inferred from the return type of the callback function. since tuple types are never inferred unless there is a contextual type (which in this case there is none), a regular array type is inferred forU, then the assignment check is done, and it fails.The piece that is missed here, is we know that the
U[]will be assigned tomappedArr, and we should use this as a contextual type. #11152 tracks propagating using the return type of the contextual signature if one exists as another location for inference.