TypeScript Reference ยท Generics
Default type parameters in TypeScript
Reference for TypeScript default type parameters: give generics a fallback type with =, order optional parameters last, and combine defaults with constraints.
A default type parameter supplies a fallback type for a generic when the caller gives no type argument and the compiler has nothing to infer. You write it with = after the parameter: <T = string>. Callers can then leave the argument out, which keeps common cases short while still allowing customization.
Defaults work on interfaces, type aliases, classes and functions. They are most valuable on types that are written by hand in many places, such as Result<T, E = Error>, Promise-like wrappers and event maps.
interface Response<TBody = unknown, TError = Error> { body?: TBody; error?: TError; } let r: Response; // Response<unknown, Error> let s: Response<string>; // Response<string, Error>
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
type Name<T = Default> = ... interface Name<T, U = T> { ... } class Name<T extends Base = DefaultBase> { ... } function name<T = Default>(...): ...
| Part | Description |
|---|---|
= Default |
The type used when no type argument is written and inference finds no candidate. |
| Order | Parameters with defaults must come after all required parameters (TS2706). |
| Reference | A default may refer to earlier parameters (B = A) but not later ones. |
With extends |
Write the constraint first, then the default: T extends object = {}. The default must satisfy the constraint. |
Edge cases: you may supply fewer type arguments than parameters only if the missing ones have defaults. Giving explicit arguments to some parameters of a function disables inference for the others, which then fall back to their defaults.
Usage notes
- Interfaces and aliases with all-default parameters can be written bare:
Emitter,Result. The angle brackets are only needed to change the defaults. - Defaults are not inference candidates. In a function, inference from the arguments happens first. The default fills in only what could not be inferred.
- Partial explicit arguments.
fn<string>(...)on a function with two parameters will not infer the second; it uses its default or reports an error. TypeScript has no partial inference. - A good default is the most permissive useful type.
unknownis usually safer thanany. - Changing a default is a breaking change for code that relied on the bare form, because every bare use silently changes meaning.
- Class type parameter defaults also apply to
extendsclauses:class Child extends Base {}usesBase's defaults. - Defaults vanish at runtime, like all type information.