TypeScript-Handbook
TypeScript-Handbook copied to clipboard
Incorrect type for destructuring an array in function parameters
In the handbook section on Array Destructuring the type is incorrect for the function parameter.
input
is defined this way:
let input = [1, 2];
Therefore this throws a compiler error since it is expecting a Tuple (see playground example):
function f([first, second]: [number, number]) {
console.log(first);
console.log(second);
}
f(input);
Argument of type 'number[]' is not assignable to parameter type of '[number, number]'. Property '0' is missing in type 'number[]'
It should look like this instead:
function f([first, second]: number[]) {
console.log(first);
console.log(second);
}
f(input);
We should switch the call to;
f([1,2]);
to get the contextual type.
PRs are welcomed.
If I'm not mistaken, this is not an issue in the current handbook anymore.