Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A default parameter in TypeScript allows a formal parameter to be initialized with a specific value if the caller omits the corresponding argument or explicitly passes undefined. It guarantees that the parameter will have a defined value within the function’s execution context while simultaneously informing the TypeScript compiler about the parameter’s type and optionality.

Syntax and Type Inference

When a default value is assigned, TypeScript can automatically infer the parameter’s type. Explicit type annotations are permitted but often redundant unless defining a union type.
// Explicit type annotation
function initializeExplicit(timeout: number = 3000): void {}

// Implicit type inference (timeout is inferred as 'number')
function initializeImplicit(timeout = 3000): void {}

// Explicit union type with a default
function initializeUnion(timeout: number | "infinite" = 3000): void {}

Implicit Optionality

Assigning a default value implicitly marks the parameter as optional. You do not need to append the ? modifier to the parameter name. In the generated declaration files (.d.ts), TypeScript represents default parameters as optional.
function connect(protocol = "http") {}

// The inferred signature is:
// function connect(protocol?: string): void;

connect();       // Valid: protocol becomes "http"
connect("wss");  // Valid: protocol becomes "wss"

The undefined vs. null Behavior

Default parameters are triggered strictly by omission or by passing the exact value undefined. Passing null, false, 0, or "" will bypass the default assignment, as these are considered valid, provided arguments.
function setThreshold(limit = 50) {
  console.log(limit);
}

setThreshold();          // Evaluates to 50 (omitted)
setThreshold(undefined); // Evaluates to 50 (explicit undefined)
// setThreshold(null);   // Evaluates to null (Type error if strictNullChecks is enabled)
setThreshold(0);         // Evaluates to 0

Parameter Ordering

Unlike standard optional parameters (using ?), which must appear at the end of the parameter list, parameters with default values can be positioned anywhere. However, if a default parameter precedes a required parameter, the caller must explicitly pass undefined to trigger the default value.
function configure(port = 8080, host: string) {}

// Invalid: Expected 2 arguments, but got 1.
// configure("localhost"); 

// Valid: Explicitly passing undefined triggers the default 'port'
configure(undefined, "localhost"); 

Runtime Evaluation

Default parameters are evaluated at runtime, not compile time. The default value can be an expression, a function call, or a reference to preceding parameters in the same signature.
// Evaluated per-call
function generate(id = Math.random()) {}

// Referencing preceding parameters (Temporal Dead Zone rules apply)
function calculateArea(width: number, height = width * 2) {}
Master TypeScript with Deep Grasping Methodology!Learn More