Skip to main content
An optional parameter in TypeScript is a function, method, or constructor parameter that is not strictly required during invocation. By appending a question mark (?) to the parameter identifier in the signature, the TypeScript compiler permits the caller to omit the argument, implicitly passing undefined in its place.

Type System Behavior

When a parameter is marked as optional, TypeScript automatically unions its declared type with undefined. Assuming --strictNullChecks is enabled in the tsconfig.json, a parameter declared as param?: string is evaluated by the type checker as string | undefined. Consequently, the compiler enforces type narrowing within the function body, requiring you to handle the undefined state before invoking methods specific to the declared type.

Structural Constraints

TypeScript enforces strict positional rules for optional parameters. All optional parameters must appear after all required parameters in the parameter list. A required parameter cannot follow an optional parameter.

Interaction with Default Parameters

Parameters assigned a default value via an initializer (=) are inherently optional to the caller. However, you cannot combine the optional modifier (?) with a default initializer.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More