Sub topic 4
How the Compiler Picks an Overload
Assume you know basic TypeScript types, unions, literal types, typed functions, typeof narrowing, Array.isArray, classes, and interfaces. This chapter focuses on overload resolution: how TypeScript compares a call with overload signatures and chooses the type of the result.
Overloads are checked from top to bottom
An overloaded function has one or more overload signatures followed by one implementation signature. Callers can use only the overload signatures. TypeScript checks those signatures in the order they appear.
function format(value: Date): string;
function format(value: number): string;
function format(value: Date | number): string {
return value instanceof Date
? value.toISOString()
: value.toFixed(2);
}
const dateText = format(new Date()); // string
const numberText = format(12.5); // stringFor each call, TypeScript looks for an overload whose parameters can accept the supplied arguments. When it finds a matching overload, it uses that overload's return type. The implementation signature is not considered a choice for the caller.
The compiler's basic process is:
- Read the call's argument count and types.
- Try the overload signatures from top to bottom.
- Check whether an overload can accept the arguments.
- Select the first applicable overload.
- Give the call the selected overload's return type.
Put specific signatures before general signatures
A general signature can accept values that a more specific signature also accepts. If the general signature comes first, TypeScript stops there and never reaches the specific one.
function describe(value: string): "text";
function describe(value: string | number): "text" | "number";
function describe(value: string | number): "text" | "number" {
return typeof value === "string" ? "text" : "number";
}
const result = describe("hello"); // "text"The first overload is more specific, so the result has the literal type "text".
Reversing the overloads changes the result:
function describe(value: string | number): "text" | "number";
function describe(value: string): "text";
function describe(value: string | number): "text" | "number" {
return typeof value === "string" ? "text" : "number";
}
const result = describe("hello"); // "text" | "number"The first overload already accepts a string. TypeScript selects it without checking the second overload.
Use this ordering rule:
| Signature order | Result for a string argument | Column |
|---|---|---|
| Specific overload, then general overload | `"text"` | |
| General overload, then specific overload | `"text" | "number"` |
A useful way to identify the correct order is to ask whether every value accepted by one overload is also accepted by another. Put the narrower set first and the wider set later.
Argument types must match the overload
TypeScript checks assignability, which means it asks whether each argument can safely be used where the overload's parameter type is expected.
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[]A value whose type is a union does not automatically match either individual overload.
declare const value: string | number;
// Error: No overload matches this call.
// toArray(value);At runtime, value is either a string or a number. However, the compiler cannot choose between the two overloads. The string overload requires a definite string, and the number overload requires a definite number. The union overload is missing.
Add an overload that describes the union case if callers need it:
function toArray(value: string): string[];
function toArray(value: number): number[];
function toArray(value: string | number): (string | number)[];
function toArray(value: string | number): (string | number)[] {
return [value];
}
declare const value: string | number;
const result = toArray(value); // (string | number)[]The added overload is visible to callers. The implementation signature only explains how the function body handles the values.
Argument count is part of resolution
Overloads can also distinguish calls by their number of arguments.
function createDate(timestamp: number): Date;
function createDate(year: number, month: number, day: number): Date;
function createDate(
first: number,
second?: number,
third?: number
): Date {
if (second === undefined || third === undefined) {
return new Date(first);
}
return new Date(first, second - 1, third);
}
const fromTimestamp = createDate(0);
const fromParts = createDate(2026, 11, 25);
// Error: no overload takes exactly two arguments.
// const invalid = createDate(2026, 11);The optional parameters belong to the implementation because the body must support both forms. They do not create a public two-argument form. Only the two overload signatures define valid calls.
Generic overloads and ordering
Generic overloads can provide precise relationships between input and output types. A broad overload can accidentally hide a more useful generic one, so order the more informative signature first.
function getValue<T>(values: readonly T[], index: number): T;
function getValue(values: readonly unknown[], index: number): unknown;
function getValue(
values: readonly unknown[],
index: number
): unknown {
return values[index];
}
const value = getValue(["a", "b"], 0); // string
const unknownValue = getValue([true, false] as unknown[], 0); // unknownFor the first call, TypeScript infers T as string and uses the generic overload. The second argument is typed as unknown[], so the result is unknown.
When overloads have overlapping parameter types, do not depend on readers to infer which one wins. Place the intended first match first and test important calls with type-checking.
Do not confuse resolution with conditional-type inference
Overload resolution chooses a signature for an actual call. Conditional types such as ReturnType use a different rule for an overloaded function. Inference generally uses the last signature, which should usually be the broad implementation-like signature.
function parse(input: string): number;
function parse(input: string[]): number[];
function parse(input: string | string[]): number | number[] {
if (Array.isArray(input)) {
return input.map(Number);
}
return Number(input);
}
const one = parse("42"); // number
const many = parse(["1", "2"]); // number[]
type Parsed = ReturnType<typeof parse>; // number | number[]The calls use the first matching overload. ReturnType<typeof parse> does not simulate every possible call. It reflects the broad final signature.
This difference matters when you expose an overloaded function through another type utility. Check both actual calls and extracted types.
A practical decision process
When deciding whether a call will match:
- Count the arguments.
- Compare each argument with the first overload's parameter types.
- If any required argument fails, continue to the next overload.
- Stop at the first overload that accepts all arguments.
- Use that overload's return type.
- If none matches, report an error. The implementation signature cannot rescue the call.
Keep overloads ordered from most specific to most general. If several signatures have the same parameters but different return types, make sure the order reflects the result you want callers to see.
Exercise
Given these declarations, what is the type of result, and why? function read(id: string): string; function read(id: string | number): string | number; function read(id: string | number): string | number { return id; } const result = read("x");
Show answer
The type is string. Both overloads can accept a string, but the first overload is checked first and is selected because it is more specific.