TypeScript · Middle · Question 10 of 75
How do function overloads work in TypeScript?
function parse(x: string): number;
function parse(x: number): string;
function parse(x: string | number) { /* ... */ }- ✓Correct answer: Several call signatures and one hidden implementation
- 2Each signature gets its own function implementation, as in Java
- 3The last signature overrides all previous ones, which are ignored
Explanation
Calling code sees only the overload signatures, and TS picks the first matching one from top to bottom. The implementation must be compatible with all of them. Overloads can often be replaced with a union parameter or a generic with a conditional type.