In languages like Java or C#, function and method overloading is a standard feature: you can declare multiple functions with the same name but different parameters. JavaScript does not have this, which adds potential errors. TypeScript addresses this at the type level.
TypeScript does not have true overloading (multiple implementations). You can declare multiple signatures for one function, but only one implementation can be provided. Errors arise when the declared signatures and implementation get out of sync or when the positioning of signatures is incorrect.
TypeScript supports function overloading by declaring multiple signatures in a row and a single real implementation that is compatible with all overload variants.
Code example:
function sum(a: number, b: number): number; function sum(a: string, b: string): string; function sum(a: any, b: any): any { return a + b; } const a = sum(1, 2); // 3 const b = sum('foo', 'bar'); // "foobar"
Key features:
Can you define the function implementation before declaring all overload signatures?
No, all overload signatures must come first, followed by the implementation. Error:
// Error! Signatures first, then implementation function foo(a: number): number { return a } // error function foo(a: string): string { return a } // error
Does TypeScript check return values in all overload signatures?
TypeScript guarantees return types according to declared signatures only at compile time; no type checks occur at runtime.
Is it necessary to cover all input variations in a single implementation?
The implementation function should correctly handle all input variations according to the overload signatures:
function parse(a: number): string; function parse(a: string): string; function parse(a: number | string): string { return String(a); }
Multiple overloads of a function have been declared with different input types, but the implementation does not handle all cases and throws an error at runtime for rare inputs.
Pros:
Cons:
Each function overload is covered with type conditions within the implementation, using type checks (typeof/instanceof).
Pros:
Cons: