Skip to main content
A default type parameter in TypeScript allows a generic type, interface, class, or function to specify a fallback type for a generic parameter. If the consumer of the generic does not explicitly provide a type argument and the compiler cannot infer one from the context, TypeScript automatically assigns the default type.

Syntax

The default type is assigned using the = operator within the generic angle brackets < >.

Rules and Mechanics

When defining default type parameters, the TypeScript compiler enforces strict structural rules: 1. Ordering and Cascading Defaults Required type parameters must strictly precede optional (defaulted) type parameters. Once a default type is declared for a parameter, all subsequent type parameters in the generic list must also be initialized with a default type.
2. Constraint Satisfaction If a type parameter is bounded by a constraint using the extends keyword, the default type must strictly satisfy that constraint.
3. Forward Referencing A default type parameter can reference preceding type parameters declared within the same generic list. It cannot reference type parameters declared after it.

Resolution Order

When the TypeScript compiler processes a generic invocation, it resolves the type parameter in the following order:
  1. Explicit Assignment: The type explicitly passed in the angle brackets (e.g., Container<number>).
  2. Inference: The type inferred from the function arguments or assignment context.
  3. Default Type: The fallback type defined by the = operator in the generic declaration.
  4. Constraint Fallback: If no default exists and inference fails, it falls back to the constraint type (if extends is used) or unknown/{}.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More