TS2556

error TS2556: A spread argument must either have a tuple type or be passed to a rest parameter.

Broken Code ❌

1
2
3
4
5
function sum(a: number, b: number): number {
return a + b;
}

console.log(sum(1, ...[2, 3, 4, 5]));

Fixed Code ✔️

When calling a function using the spread syntax (...), we have to ensure that the called function uses a rest
parameter:

1
2
3
4
5
function sum(a: number, ...b: number[]): number {
return a + b.reduce((c, d) => c + d);
}

console.log(sum(1, ...[2, 3, 4, 5]));