Sub topic 3
The Implementation Signature and Why Callers Cannot See It
Assume you know basic TypeScript function types, unions, type narrowing with typeof, and how to write and call typed functions. This chapter focuses on the boundary between overload signatures and the implementation signature.
An overloaded function has two parts:
- Overload signatures describe the call shapes that callers may use.
- The implementation signature describes the parameters and return value used inside the function body.
The implementation signature makes the body possible, but it is not part of the public API that callers use.
The two kinds of signatures
Suppose a function converts either a string or a number into a one-element array:
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 form the public interface:
- A string argument produces
string[]. - A number argument produces
number[].
The third declaration contains the function body. Its parameter type is string | number, so the body can accept either kind of value. Its return type is (string | number)[], which is broad enough to cover both overload results.
The implementation signature must be compatible with every overload signature. In practice, its parameters usually accept all values accepted by the overloads, and its return type must be able to produce every declared overload result.
This separation lets the public API be precise while the implementation handles the shared runtime behavior.
Why the implementation signature is hidden
Consider this function:
function toArray(value: string): string[];
function toArray(value: number): number[];
function toArray(value: string | number): (string | number)[] {
return [value];
}A caller cannot use the union from the implementation signature directly:
declare const value: string | number;
// Error: No overload matches this call.
const result = toArray(value);The variable might contain a string or a number at runtime. However, neither visible overload accepts string | number as a whole. TypeScript does not use the implementation signature as a fallback for callers.
To support a union argument, add an overload that explicitly describes 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 new overload changes the public API. It tells callers that a union value is valid and that the result will contain values from that union.
This rule prevents accidental dependence on a private implementation detail. If TypeScript exposed the implementation signature automatically, an author could unintentionally make a broad, less precise signature part of the API.
Making the implementation compatible
The implementation must be able to handle every case promised by the overloads. TypeScript checks this relationship when the overload is declared.
Here, the implementation parameter is too narrow:
function format(value: string): string;
function format(value: number): string;
// Error: The implementation signature is not compatible
// with the overload signature.
function format(value: string): string {
return value.toUpperCase();
}The number overload promises that format(10) works, but the implementation accepts only strings. The declarations and the body disagree.
A compatible implementation accepts both types and narrows the union before using it:
function format(value: string): string;
function format(value: number): string;
function format(value: string | number): string {
if (typeof value === "string") {
return value.toUpperCase();
}
return value.toFixed(2);
}
format("hello"); // "HELLO"
format(10); // "10.00"The typeof check narrows value from string | number to string in the first branch. After that branch, TypeScript knows that value is a number.
The implementation return type is simply string because both overloads return strings. It does not need to repeat two separate return types.
A call-flow model
The compiler uses the overload declarations when checking calls, then uses the implementation signature when checking the body:
The type system checks the call before runtime execution. At runtime, JavaScript has only one function body. The overload signatures disappear after compilation.
The body is not automatically safe
The implementation signature helps TypeScript check parameter compatibility, but it does not prove that the body returns the correct result for every case. An overly broad implementation can hide a logic error.
function getValue(key: "name"): string;
function getValue(key: "age"): number;
function getValue(key: "name" | "age"): string | number {
if (key === "name") {
return "Ada";
}
return "not a number"; // TypeScript allows this
}
const age = getValue("age"); // typed as numberThe overload says that getValue("age") returns a number. The implementation return type allows string | number, so the incorrect string return is accepted. The variable age is still typed as number, even though the runtime value is a string.
TypeScript does not compare the body’s behavior against every overload result. It checks the declared implementation signature. This means overload implementations need runtime tests, careful narrowing, and sometimes explicit assertions or more precise internal types.
A safer implementation can validate the result before returning it:
function getValue(key: "name"): string;
function getValue(key: "age"): number;
function getValue(key: "name" | "age"): string | number {
if (key === "name") {
return "Ada";
}
const age = 36;
return age;
}The implementation now follows the contract for both branches.
Union overloads and implementation parameters
A common mistake is to write only overloads and forget that the body needs a type covering all cases:
function describe(value: string): string;
function describe(value: number): string;
function describe(value: string | number): string {
return typeof value === "string"
? `text: ${value}`
: `number: ${value}`;
}The union belongs in the implementation because the body receives either type at runtime. The overloads remain separate because they describe the calls that users may make.
For more complex functions, the implementation may use optional parameters:
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);
}The overloads expose one-argument and three-argument calls. The optional parameters let the implementation represent both forms. A two-argument call still fails because no overload declares it.
Choosing the implementation signature
Use these checks when writing an overloaded function:
- Does the implementation parameter type accept every overload argument?
- Does the implementation return type cover every overload return type?
- Does the body narrow unions before using type-specific operations?
- Does every runtime branch honor the promise made by its overload?
- Should a union argument have its own visible overload?
- Could a union, optional parameter, or generic express the API more simply?
The implementation signature is an internal bridge. Keep it broad enough to handle all declared cases, but keep the overload signatures precise enough to protect callers.
Exercise
Why does toArray(value) fail when value has type string | number, even though the implementation parameter also has type string | number?
Show answer
Callers can use only the visible overload signatures. The implementation signature is hidden from callers, so a separate string | number overload is needed.