- Error
- Reported by the type checker
TS2556
A spread argument must either have a tuple type or be passed to a rest parameter.
An array is spread into a call where it would fill fixed parameters. Spread a tuple type, or give the function a rest parameter.
What it means
Spreading ...args into a call is only checked when the checker knows how many elements it has (a tuple type) or when it lands in a rest parameter. Spreading an array of unknown length into fixed parameters is reported.
Example
function move(x: number, y: number): void {}
const coords = [1, 2];
move(...coords);
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,6): error TS2556: A spread argument must either have a tuple type or be passed to a rest parameter.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Give the array a tuple type:
const coords: [number, number] = [1, 2]or[1, 2] as const. - Or give the function a rest parameter:
function move(...xy: number[]).