Sub topic 8
Default Type Parameters in TypeScript
Prerequisites
This chapter assumes that you know basic TypeScript types, interfaces, type aliases, functions, unions, and generic type parameters. A default type parameter is a fallback type that TypeScript uses when code does not provide a type argument.
Defaults make generic APIs easier to call without removing the option to be explicit.
Adding a Default
Place an equals sign and a type after the parameter name:
interface ApiResponse<T = unknown> {
data: T;
status: number;
}
const raw: ApiResponse = {
data: "value",
status: 200,
};
const typed: ApiResponse<string[]> = {
data: ["a", "b"],
status: 200,
};ApiResponse has one type parameter, T. When the type is omitted, T becomes unknown. When the caller supplies string[], that explicit type replaces the default.
Using unknown is often safer than using any. You can store any value in unknown, but you must check its type before using it.
Defaults and Inference
A default is used only when TypeScript cannot infer a more specific type. Inferred and explicit types take priority over the default.
function createList<T = string>(first?: T): T[] {
return first === undefined ? [] : [first];
}
const names = createList(); // string[]
const numbers = createList(42); // number[]
const flags = createList<boolean>(); // boolean[]The first call provides no value and no type argument, so T uses its default, string. The second call gives TypeScript a number, so it infers T as number. The third call explicitly sets T to boolean.
This behavior lets a common case stay short while preserving precise types for less common cases.
flowchart TD
A[Call generic API] --> B{Type argument supplied?}
B -->|Yes| C[Use explicit type]
B -->|No| D{Can TypeScript infer a type?}
D -->|Yes| E[Use inferred type]
D -->|No| F[Use default type]Rules for Multiple Parameters
A required type parameter cannot come after an optional type parameter. Once a parameter has a default, later parameters must also have defaults.
type Result<Data = unknown, ErrorInfo = string> = {
data: Data;
error?: ErrorInfo;
};
const result: Result<number> = {
data: 200,
};
const detailed: Result<number, { code: number }> = {
data: 200,
error: { code: 500 },
};Here, Data defaults to unknown and ErrorInfo defaults to string. The first type argument supplies Data, while ErrorInfo keeps its default. The second example supplies both arguments.
A type parameter with a default is optional at the call site. It is still a real type parameter, so it can connect several properties or operations in the generic declaration.
Common Errors
Do not use a default when every caller should make an intentional choice. A default can hide a missing decision.
Also remember that a default does not constrain a type. In ApiResponse<T = unknown>, callers can still provide any type for T. Use extends when you need to restrict allowed types.
Exercise
What is the type of value in const value = createList(10); when createList<T = string>(first?: T): T[] is used?
Show answer
value has type number[]. TypeScript infers T as number, so the default string is not used.