Sub topic 12
Unions and Intersections of Function Types
Assume you know basic TypeScript type aliases, function types, and union and intersection types. This chapter focuses on what happens when the types being combined are functions. The rules differ from object types, so function unions and intersections often cause surprising errors.
A union of functions
A union means that a value can have one of several types:
type StringOrNumberHandler =
| ((value: string) => string)
| ((value: number) => string);
const upperCase: (value: string) => string = value => value.toUpperCase();
const fixedPoint: (value: number) => string = value => value.toFixed(2);
const handler: StringOrNumberHandler =
Math.random() > 0.5 ? upperCase : fixedPoint;
// handler("42"); // Error: no argument is safe for every possible handlerAt the call site, handler might refer to either function. A string is valid for upperCase, but not for fixedPoint. A number is valid for fixedPoint, but not for upperCase.
Therefore, a union of functions can be called only with arguments accepted by every member of the union. For these two parameter types, that common requirement is effectively string & number, which is never. never is a type with no possible value.
The return type follows a different rule. If a call is possible, the result may have the return type from any member, so TypeScript generally produces a union of the possible return types.
The union can still be useful when you do not call the function directly, such as when storing callbacks in a data structure or passing them through generic code. To call it safely, preserve information that identifies which function you have, or use a common function signature.
An intersection of functions
An intersection requires a value to satisfy all the combined types. For functions, this behaves similarly to overloads: one function supports several call signatures.
type Converter =
& ((value: string) => number)
& ((value: number) => string);
function convert(value: string): number;
function convert(value: number): string;
function convert(value: string | number): number | string {
if (typeof value === "string") {
return Number(value);
}
return value.toString();
}
const converter: Converter = convert;
console.log(converter("42")); // 42
console.log(converter(42)); // "42"The implementation signature accepts string | number, but callers see the two overload signatures. TypeScript chooses the matching signature from the argument:
| Function type | Meaning at a call site |
|---|---|
| Union of functions | The argument must work for every possible function |
| Intersection of functions | The argument selects one compatible call signature |
The order of overload signatures can matter when more than one signature matches. Put specific signatures before broad signatures. Also remember that the implementation signature is not directly visible to callers.
How the two forms differ
Use a function union when the value truly may be one of several unrelated callbacks and callers should not assume which one it is. Use an intersection, usually written as overloads, when one implementation intentionally supports several input shapes.
A common error is to use a union when the intended API is overloaded:
type Wrong = ((value: string) => number) | ((value: number) => string);
// A Wrong value cannot safely be called with either string or number.
type Right = ((value: string) => number) & ((value: number) => string);
// A Right value supports both call signatures.In application code, overload declarations are often clearer than writing an intersection of function types directly. Both describe multiple callable signatures, but overloads can provide better documentation and more precise implementation checking.
Exercise
Why can a value of type ((x: string) => number) | ((x: number) => string) not safely be called with a string?
Show answer
The value might be the number-based function, so the string is not valid for every member of the union. A function intersection or overload is needed when one value should support both calls.