the tswhy logo: a question mark in a box

tswhy‽

A community effort to enrich TypeScript diagnostics.

Editing TS1015:

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.

TS1015

Parameter cannot have question mark and initializer.

If a parameter is marked as optional with ?, it means that passing undefined is acceptable. If a parameter is marked as optional by providing an initializer, it communicates to readers that if not provided (or set to undefined) the default will be used. It doesn't make sense to use both modifiers.

function test(a?: number = 0) {}

Fix: Remove the question mark or the initializer

Remove the question mark if the default better communicates your intent, or remove the initializer:

function test(a: number = 0) {}
// or
function test(a?: number) {}