TypeScript Generics and Advanced Type Operators
Generic default type arguments
A default supplies a type argument when inference does not select one and the caller has not supplied one. A default must satisfy any constraint, and required parameters cannot follow optional/defaulted parameters. Defaults do not supply runtime argument values. A default may depend on an earlier type parameter. Example output: "ready 7". Local strict TypeScript 5.8.3 / ES2022 baseline. The type construct itself is erased; the logged value illustrates a checked use, not runtime type validation.
Formal Syntax & Structure
typescript syntaxtypescript
type Envelope<T = string> = { data: T };Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| T | defaulted type parameter | Uses string in the illustrated alias if omitted. |
Working Example
Example Snippettypescript
type Envelope<T = string> = { data: T };const ordinary: Envelope = { data: "ready" };const numeric: Envelope<number> = { data: 7 };console.log(ordinary.data, numeric.data);