Sub topic 8
Overloads for Functions with Several Call Shapes
Assume you know basic TypeScript functions, object types, unions, callbacks, and how to run tsc. This chapter focuses on functions that support more than one valid way to call them.
A function overload gives callers several precise call signatures. Each signature describes one allowed call shape. The implementation below the overloads contains the runtime code.
function toArray(value: string): string[];
function toArray(value: number): number[];
function toArray(value: string | number): (string | number)[] {
return [value];
}
const words = toArray("hello"); // string[]
const numbers = toArray(42); // number[]The first two declarations are overload signatures. They have no function body. The final declaration is the implementation signature and contains the body.
The compiler uses the overload signatures when checking calls. Therefore, words is string[] and numbers is number[]. The implementation signature is not visible to callers.
How overload resolution works
When TypeScript sees a call, it checks the overloads from top to bottom. It selects the first compatible signature.
function format(value: Date): string;
function format(value: number): string;
function format(value: Date | number): string {
if (value instanceof Date) {
return value.toISOString();
}
return value.toFixed(2);
}
format(new Date());
format(12.5);
// format("12"); // error: no overload accepts a stringThe implementation accepts Date | number, so it can handle both cases at runtime. The parameter type must be broad enough to support every overload. However, callers cannot use the implementation signature to create additional call shapes.
A common mistake is to expose only the implementation signature:
function parseValue(value: string | number): string | number {
return typeof value === "string" ? Number(value) : String(value);
}
const result = parseValue("42"); // string | numberThis version is valid, but the return type is a union even when the input is known. Overloads can give each input a more useful result type.
Overloads versus unions
Use a union when all accepted inputs have the same result type and behavior is simple to describe. Use overloads when the relationship between the input and output needs to remain precise.
| Choice | Best when | Example | Column |
|---|---|---|---|
| Union | Inputs share one result type | `format(value: Date \ | number): string` |
| Overloads | Different inputs produce different result types | `toArray(string): string[]` | |
| Generic | The output type follows an input type | `first<T>(items: T[]): T` |
Overloads also work well when the call shapes have different numbers of parameters.
function buildLabel(name: string): string;
function buildLabel(name: string, count: number): string;
function buildLabel(name: string, count?: number): string {
return count === undefined ? name : `${name} (${count})`;
}
buildLabel("Apples");
buildLabel("Apples", 3);
// buildLabel(); // error: no overload matches this callThe optional parameter belongs to the implementation. The public API is defined by the overload signatures.
Common errors
The implementation signature must be compatible with every overload. This usually means using a union, an optional parameter, or a broad type such as unknown.
function getLength(value: string): number;
function getLength(value: string[]): number;
function getLength(value: string | string[]): number {
return typeof value === "string" ? value.length : value.length;
}Do not add a call that matches only the implementation signature:
function toValue(value: string): string;
function toValue(value: number): number;
function toValue(value: string | number): string | number {
return value;
}
// toValue(true); // error, even though the implementation type is not visibleOverloads can become difficult to maintain when there are many combinations. Prefer a single options object when arguments are strongly related or when the API has many optional choices.
Exercise
Why does toArray("x") have type string[] when the implementation returns (string | number)[]?
Show answer
Callers are checked against the overload signatures, and the matching overload for a string promises string[]. The implementation signature is used internally and is not exposed as the public call type.