the tswhy logo: a question mark in a box

tswhy‽

A community effort to enrich TypeScript diagnostics.

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) {}