Take a look at these two straightforward functions written in typescript:
function generate(): Array<[number, number]> {
return [[0, 1], [2, 3]];
}
function mutate(input: Array<[number, number]>): Array<[number, number]> {
return input.map(xy => [xy[0] + 1, xy[1] + 1]);
}
console.log(mutate(generate()));
The function mutate
transforms an array of arrays containing two integers into another similar array. The TypeScript compiler shows the following error message:
Type 'number[]' is not assignable to type '[number, number]'.
Property '0' is missing in type 'number[]'.
Should I modify the function's signature to number[][]
(potentially allowing accidental use of different types of arrays) or should I utilize an interface? Is there a way to define the exact return type of the mapping function as Array<[number, number]>
?
Edit1: Thanks to @Titian Cernicova-Dragomir for pointing out that the current version of Typescript does not display that error - but version 3.1.1 does.