Default Type Parameters
A default type parameter fills a generic hole when the caller does not specify it and inference cannot either. You write it as T = Fallback after any constraints.
Detailed Explanation
Generic inference is good at reading arguments. It is bad at reading your mind when there are no arguments that mention T. function empty<T>(): T[] { return []; } has nothing to infer from. Callers must write empty<string>() or the parameter becomes unknown (or a hard error, depending on context).
A default gives that call a meaning: function empty<T = never>(): T[] { return []; }. empty() is then never[], which is a reasonable empty list until you push into an annotated target. function createEvent<T = unknown>(payload?: T) makes createEvent() an event whose payload you have not described, and createEvent({ id: 1 }) still infers T from the argument.
Defaults follow the same ordering rule as default function parameters: once one parameter has a default, every parameter after it needs a default or must be inferable from elsewhere. type Api<TResponse, TError = Error> is valid. type Api<TResponse = unknown, TError> is not, because a required parameter sits after a defaulted one.
A default can refer to an earlier parameter. type Pair<T, U = T> = [T, U] makes the second type match the first unless the caller overrides it. Partial<T>-style utilities do not need this; container types often do. React.FC historically defaulted P = {}. Promise does not default T, because an unresolved element type is more dangerous than an extra annotation.
Defaults do not override inference. If the compiler can see a type for T from an argument or a contextual type, it uses that type. The default is the fallback for when it cannot. That is why useState in React can be called as useState(0) (T inferred as number) or useState<number | null>(null) (the default would not have included null on its own).
Choose defaults the way you choose function parameter defaults: the value that is safe if someone forgets. T = any is the unsafe choice — it silences every subsequent check. T = unknown is the safe choice when the type is an input you have not inspected. T = never is the safe choice when the type is an output that should be impossible to produce without further information, such as an empty collection.
The cost of a default is that a missing annotation becomes harder to notice. A public generic with a default of any is an implicit any on every call site that forgot to say what T is. Prefer a default that produces an obvious follow-on error over one that compiles quietly.
Code Examples
1. A default used only when inference has nothing to read.
// 1. A default used only when inference has nothing to read.
function emptyList<T = never>(): T[] {
return [];
}
const inferred = emptyList<string>(); // string[]
const blank = emptyList(); // never[]
const names: string[] = emptyList(); // string[] — contextual type fills T2. Later parameter defaults; earlier parameter can be referenced.
// 2. Later parameter defaults; earlier parameter can be referenced.
type Result<T, E = Error> =
| { ok: true; value: T }
| { ok: false; error: E };
type OkNumber = Result<number>; // E is Error
type OkNumberOrString = Result<number, string>;
function ok<T>(value: T): Result<T> {
return { ok: true, value };
}3. Defaults must come last; inference still wins over the default.
// 3. Defaults must come last; inference still wins over the default.
type Box<T = string, U = T> = { item: T; extra: U };
const a: Box = { item: "x", extra: "y" }; // T and U are string
const b: Box<number> = { item: 1, extra: 2 }; // U defaults to number
const c: Box<number, boolean> = { item: 1, extra: true };
function wrap<T = unknown>(value?: T): { value: T | undefined } {
return { value };
}
const inferredWrap = wrap(3); // T is number, not unknownCommon Gotchas
A required type parameter cannot follow a defaulted one
`type F<T = string, U>` is a syntax error. Reorder, or give `U` a default too.
`T = any` is an implicit any at every unspecified call site
the flag `noImplicitAny` will not catch it, because the default is explicit. Prefer `unknown` or `never`.
Inference beats the default
passing an argument whose type is visible fills `T` even when a default exists. Write an explicit type argument only to *widen* or *override* that inference.
Changing a default is a breaking change for un-annotated callers
adding or tightening `T = X` changes the type of every `foo()` call that relied on the old fallback.