the tswhy logo: a question mark in a box

tswhy‽

A community effort to enrich TypeScript diagnostics.

Editing TS1016:

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.

TS1016

A required parameter cannot follow an optional parameter.

When a parameter is marked as optional with ? it indicates that callers can omit the argument when calling the function. If another parameter is required after the optional parameter, the ? would be effectively invalidated since users must pass the argument in order to provide the later required argument.

function test(a?: number, b: number) {}

Fix: Allow the argument to be undefined.

Explicitly union the first argument with undefined and omit the question mark:

function test(a: number | undefined, b: number) {}

Fix: Re-order parameters

Reorder the parameters so that required parameters appear before the optional ones:

function test(b: number, a?: number) {}