We can define multiple function declarations to define different signatures (parameters/return types) and TypeScript will infer the right signature based on call arguments.
Some gotchas:
- Implementation should narrow types based on overload
- Order matters: put most specific overloads first
Use Cases:
- Reusable functions with different types
- Library functions mimicking overloads
- Gradual typing - multiple signatures
Example:
// Overloads
function getString(opts: { fallback: string }): string;
function getString(opts: {}): string | undefined;
// Implementation
function getString(opts: {}): string | undefined {
// ...
}
// Usage
const str1 = getString({ fallback: 'hello' }); // string
const str2 = getString({}); // string | undefined
References: TypeScript Handbook