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 of a tuple with array functions causes an error. #17591
// A *self-contained* demonstration of the problem follows...// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.//// create a sample array of a tuple, of type (string, number)//consttestArray: Array<[string,number]>=[['A',3],['B',7]];//// convert them to an array of a tuple, of type (string, string, number)//constmapToArray: Array<[string,string,number]>=testArray.map(([str,num])=>[str,str,num]);
Expected behavior:
should work fine Actual behavior:
error TS2322: Type '(string | number)[][]' is not assignable to type '[string, string, number][]'.
Type '(string | number)[]' is not assignable to type '[string, string, number]'.
Property '0' is missing in type '(string | number)[]'.
Workaround
Add the result type to the map function, like this:
//// create a sample array of a tuple, of type (string, number)//consttestArray: Array<[string,number]>=[['A',3],['B',7]];//// convert them to an array of a tuple, of type (string, string, number)//constmapToArray: Array<[string,string,number]>=testArray.map(([str,num]):[string,string,number]=>[str,str,num]);
But it themes to be a problem of converting a result type by checking the return type and map them to the outer function type.
Activity
Thomas-P commentedon Aug 3, 2017
Ok, I updated to version 2.4.2 and could reproduce this in this version.
kitsonk commentedon Aug 3, 2017
Tuples are never inferred when the type is ambiguous, which it is in this case.
See: #11924 (issue comment)
There is a long discussion here about inferring literals including tuples: #10195
Thomas-P commentedon Aug 3, 2017
ok, then I will close the issue.