the tswhy logo: a question mark in a box

tswhy‽

A community effort to enrich TypeScript diagnostics.

Editing TS1048:

All diagnostics and fixes are authored in markdown. Propose any changes by editing the markdown. Additional fixes can be added. A preview of the rendered diagnostic will update when changes are made.

Once all proposed changes are made, the Propose button will submit the information and confirm raising the PR.

TS1048

A rest parameter cannot have an initializer.

Since rest parameters will always be defined, even if no arguments are passed to the function or method, it doesn't make sense for them to have initializers:

function test(...args: number[] = [1, 2, 3]) {}

Fix: Move out or destructure with initializers.

If you need the first few arguments to have initializers, you can move them out of the rest parameter or destructure the argument and default the first few members there.

function test(a = 1, b = 2, c = 3, ...rest: number[]) {}
// or
function test(...[a = 1, b = 2, c = 3, ...rest]: number[]) {}