TypeScript-Handbook icon indicating copy to clipboard operation
TypeScript-Handbook copied to clipboard

Incorrect type for destructuring an array in function parameters

Open cbier opened this issue 8 years ago • 3 comments

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);

cbier avatar Apr 20 '16 13:04 cbier

We should switch the call to;

f([1,2]);

to get the contextual type.

mhegazy avatar Apr 20 '16 20:04 mhegazy

PRs are welcomed.

mhegazy avatar Apr 20 '16 20:04 mhegazy

If I'm not mistaken, this is not an issue in the current handbook anymore.

inyono avatar Oct 27 '18 12:10 inyono