Sub topic 3
The implementation signature and why callers cannot see it
An overloaded function has two layers. The overload signatures are the public contract that callers see. The implementation signature sits underneath them, next to the function body, and callers cannot use it. This chapter covers how the two layers relate and which rules connect them. It also covers the mistakes that follow from the fact that the compiler checks them against each other only loosely.
This chapter assumes you can write overload signatures, as in the previous chapter. It also assumes you are comfortable with union types and with narrowing by typeof and Array.isArray.
The three parts of an overloaded function
An overloaded function declaration has two kinds of pieces:
- Overload signatures are declarations with no body, each ending in a semicolon. They describe the ways callers may call the function.
- The implementation is the one declaration with a body. Its parameter and return types form the implementation signature.
Here is the example from the previous chapter, with the pieces labeled:
// Overload signatures: the public contract.
function toArray(value: string): string[];
function toArray(value: number): number[];
// Implementation signature and body: the private part.
function toArray(value: string | number): (string | number)[] {
return [value];
}
const a = toArray("x"); // string[]
const b = toArray(1); // number[]The implementation exists for one reason: JavaScript has no overloads. At runtime there is only one function, and it must accept every call shape. The overload signatures exist only in the type system and are erased when TypeScript compiles to JavaScript. The compiled output of the code above is a single function with a single return [value].
The diagram shows how the layers relate. Calls reach the overload signatures. Only after a call type-checks does it go to the implementation, and the caller never sees that signature.
Callers cannot see the implementation signature
The implementation signature above accepts string | number. You might expect this call to work:
declare const v: string | number;
// Error: No overload matches this call.
toArray(v);It fails because the implementation signature is not a candidate during overload resolution. The type of toArray, as seen from outside, is only the list of overload signatures. Neither (value: string) nor (value: number) accepts string | number, so the call is rejected. The same is true in your editor: hovering over toArray shows two signatures, and the implementation signature never appears.
This is deliberate. The implementation signature is usually wider and less precise than the contract you want to publish. If callers could use it, they would get back (string | number)[] and the precision you wrote the overloads to provide would be lost. Hiding the implementation signature lets you write a loose, convenient type for the body and a tight, accurate type for the public API.
The cost is that every call shape you want to allow must have its own overload. If you want toArray(v) to work with a union, add a signature that accepts it. Put it last, so that the specific signatures are tried first:
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 v: string | number;
const c = toArray(v); // string[] | number[]The third signature has the same parameter type as the implementation. That is allowed, and it does not make the implementation visible. It is simply a third overload that happens to look like the implementation signature.
The compatibility rule
The compiler does not let you write an implementation that has nothing to do with the overloads. Each overload signature must be compatible with the implementation signature. If one is not, TypeScript reports error 2394, "This overload signature is not compatible with its implementation signature", on that overload.
Compatible means, roughly, that the implementation can accept every argument list the overload allows, and that the return types are related. In practice:
- Each parameter type of the implementation must accept the overload's parameter type.
- The implementation must not require more arguments than the overload provides.
- The overload's return type and the implementation's return type must be related: one must be assignable to the other.
Here is a failure on the parameter rule:
function show(x: string): void;
// Error: This overload signature is not compatible with its implementation signature.
function show(x: boolean): void;
function show(x: string | number): void {
console.log(x);
}The second overload promises that show(true) is valid. The implementation says it accepts only string | number, so a boolean could reach the body and break it. The compiler rejects the overload.
The argument-count rule works the same way:
// Error: This overload signature is not compatible with its implementation signature.
function area(side: number): number;
function area(width: number, height: number): number;
function area(width: number, height: number): number {
return width * height;
}The first overload allows one argument, but the implementation requires two. The fix is to make the extra parameter optional in the implementation and handle undefined in the body:
function area(side: number): number;
function area(width: number, height: number): number;
function area(width: number, height?: number): number {
return width * (height ?? width);
}
area(3); // 9
area(3, 4); // 12This is the same technique the createDate example from the Topic Spine uses: the implementation covers every allowed argument count by using optional parameters.
What the return type check allows
The return type check is deliberately loose. An overload that returns number is compatible with an implementation that returns number | string, because number is assignable to number | string. An implementation that returns unknown or any is compatible with every overload. The check fails only when the two return types are unrelated, for example an overload returning string[] and an implementation returning number.
| Overload signature | Implementation signature | Compatible? | Why | Column | Column | Column |
|---|---|---|---|---|---|---|
| `(x: string): string[]` | `(x: string \ | number): (string \ | number)[]` | Yes | The parameter is accepted, and `string[]` is assignable to `(string \ | number)[]`. |
| `(x: boolean): void` | `(x: string \ | number): void` | No | The implementation does not accept `boolean`. | ||
| `(x: number): number` | `(x: number, y: number): number` | No | The implementation requires an argument the overload does not provide. | |||
| `(x: number): number` | `(x: number, y?: number): number` | Yes | The extra parameter is optional. | |||
| `(x: string): string[]` | `(x: string): number` | No | The return types are unrelated. | |||
| `(x: string): string[]` | `(x: string): unknown` | Yes | `string[]` is assignable to `unknown`. |
The check is one-directional and loose
The compatibility rule checks each overload against the implementation signature. It does not check that the body does what each overload promises. That gap is the main hazard of overloads.
Consider this version of toArray:
function toArray(value: string): string[];
function toArray(value: number): number[];
function toArray(value: string | number): (string | number)[] {
return [String(value)]; // wrong for the number overload
}
const n = toArray(1); // typed number[], but at runtime it is ["1"]
const total = n[0] + 1; // typed number; at runtime "1" + 1 is "11"This compiles without errors. The body returns string[], which is a valid (string | number)[], so the implementation is self-consistent. Each overload is compatible with the implementation signature. Nothing in the compiler connects the number overload to the fact that the body stringifies its input. The public type says number[], and the runtime value is string[].
You can reduce the risk in three ways:
- Keep the body small, and make each branch correspond to one overload.
- Narrow the parameter explicitly, so that each branch returns the right kind of value.
- Write tests that call every overload and check the returned values, not just their types.
Writing a body that handles every overload
Inside the body, the parameter has the implementation's wide type. To act differently per overload, narrow it. Here is a complete function with two array-versus-scalar overloads:
function parseValue(input: string): number;
function parseValue(input: string[]): number[];
function parseValue(input: string | string[]): number | number[] {
if (Array.isArray(input)) {
return input.map(Number);
}
return Number(input);
}
const one = parseValue("42"); // number
const many = parseValue(["1", "2", "3"]); // number[]
console.log(one, many); // 42 [ 1, 2, 3 ]The implementation parameter type is the union of the overload parameter types. The implementation return type is the union of the overload return types. Narrowing with Array.isArray gives each branch the right input type. This is the conventional shape for an implementation signature.
For overloads that differ in argument count, use optional parameters and check for undefined:
function createDate(timestamp: number): Date;
function createDate(year: number, month: number, day: number): Date;
function createDate(a: number, b?: number, c?: number): Date {
if (b === undefined || c === undefined) {
return new Date(a);
}
return new Date(a, b - 1, c);
}
console.log(createDate(0).toISOString()); // 1970-01-01T00:00:00.000Z
console.log(createDate(2026, 11, 25).getDate()); // 25The implementation signature accepts createDate(2026, 11), with two arguments, but callers cannot make that call because no overload allows it:
createDate(2026, 11); // Error: No overload matches this call.This is the clearest case of the implementation signature being more permissive than the public contract. Inside the body, the code still has to cope with b present and c missing, even though no valid call produces that. Here the body treats it like the one-argument case and silently ignores b. That is acceptable for an unreachable state, but you must decide what the body does there.
Where the implementation must go
The implementation has placement rules:
- It must immediately follow the overload signatures. Putting other statements between the overloads and the implementation produces error 2391, "Function implementation is missing or not immediately following the declaration."
- It must exist. In a regular
.tsfile, a list of overload signatures with no body is an error. The exception is an ambient declaration, written withdeclareor inside a.d.tsfile. In those, there is no implementation at all, only the signatures. - All signatures must agree on export. Marking one overload
exportand another not produces an error. The implementation joins the same group.
export function clamp(x: number): number;
export function clamp(x: number, max: number): number;
export function clamp(x: number, max: number = 100): number {
return Math.min(x, max);
}A default value works like an optional parameter here. The implementation accepts one or two arguments, so both overloads are compatible with it.
The implementation signature is not part of the type
Because callers see only the overloads, the type you get from typeof also contains only the overloads:
type ToArray = typeof toArray;
// (value: string) => string[]
// (value: number) => number[]
// (value: string | number) => string[] | number[] (only if you declared it as an overload)This has a practical consequence when you pass an overloaded function where a function type is expected. The assignment succeeds if some overload is compatible with the target type. Generic helpers such as Parameters<typeof f> and ReturnType<typeof f> read only the last overload, which a later chapter discusses under inference pitfalls. The implementation signature is never read, even if it has the widest, most informative types.
Choosing implementation types
You have some freedom in how you write the implementation signature. These are the common choices, from safest to loosest:
| Implementation style | Example | Trade-off | Column | Column |
|---|---|---|---|---|
| Union of the overload types | `(x: string \ | string[]): number \ | number[]` | Narrowing in the body is checked. This is the best default. |
| `unknown` parameters and return | `(x: unknown): unknown` | Always compatible. You must narrow everything, and the return type is unchecked. | ||
| `any` parameters and return | `(x: any): any` | Always compatible. The body is not type-checked at all. |
Use the union style unless the union gets unwieldy. Reach for unknown when the overloads are intentionally open-ended, such as pick(x: string): string followed by pick(x: unknown): unknown. Avoid any, which turns off checking inside the body, exactly where the overloads already weaken it.
A larger worked example
This function reads a setting. With a default value, it never returns undefined. Without one, it may. That is the classic return type that depends on arguments:
const settings: Record<string, string> = { theme: "dark" };
function getSetting(key: string): string | undefined;
function getSetting(key: string, fallback: string): string;
function getSetting(key: string, fallback?: string): string | undefined {
const value = settings[key];
return value !== undefined ? value : fallback;
}
const maybeTheme = getSetting("theme"); // string | undefined
const lang = getSetting("lang", "en"); // string
console.log(maybeTheme, lang); // dark enLook at what the compiler checks here. Both overloads are compatible with the implementation: the optional fallback covers both argument counts, and both return types are assignable to string | undefined. What it does not check is the promise of the second overload, that the result is always a string. If you changed the last line of the body to return value;, dropping the fallback, the code would still compile, and getSetting("lang", "en") would be typed string while returning undefined at runtime.
The implementation return type string | undefined is wider than the second overload promises. That is allowed because the compatibility check is loose. The relationship between "fallback was supplied" and "result is a string" exists only in your reasoning and your tests.
Exercise
This code has a compile error. Which signature does the compiler complain about, and what is the smallest change to fix it without changing the public overloads? function wrap(x: string): string[]; function wrap(x: number, times: number): number[]; function wrap(x: string | number, times: number): (string | number)[] { return Array(times).fill(x); }
Show answer
The compiler reports error 2394 on the first overload, wrap(x: string). That overload allows one argument, but the implementation requires two. Make the second parameter optional in the implementation: function wrap(x: string | number, times?: number): (string | number)[]. Then handle undefined in the body, for example by using const n = times ?? 1 and returning Array(n).fill(x). The second overload stays compatible, because an optional parameter accepts an explicit number.
Exercise
A colleague says "toArray accepts string | number, so I can call it with a value of that type." The overloads are toArray(value: string) and toArray(value: number), and the implementation signature is toArray(value: string | number). What is wrong with the claim, and what are two ways to support the call?
Show answer
The implementation signature is not visible to callers, so only the two overload signatures are candidates, and neither accepts a union. The call fails with "No overload matches this call." You can add a third overload that accepts string | number, placed after the specific ones, with a return type such as string[] | number[]. Or you can replace the overloads with a generic or a single union-typed signature, if the return type does not need to depend on which member of the union was passed.
Exercise
An overloaded function compiles with no errors, yet one overload returns the wrong kind of value at runtime. Why did the compiler not catch it, and what can you do about it?
Show answer
The compiler checks each overload against the implementation signature, not against the body's behavior per overload. The body is checked only against the implementation signature, which is usually a wide union, so a value that is wrong for one overload is still a valid value for the wide type. To guard against this, keep each branch of the body tied to one overload, narrow the parameter explicitly, avoid any in the implementation signature, and write runtime tests that call every overload.