Background:
TypeScript supports method overloading, similar to other strictly typed languages (like Java or C#), but the syntax for overloading in TypeScript is conceptually different. It allows multiple signatures, but only one implementation. This can confuse developers familiar with classical overloading.
Issue:
A common mistake is attempting to define multiple methods with different sets of parameters. This results in a compilation error, as TypeScript requires one implementation that fulfills all signature variations.
Solution:
Overloading is achieved by declaring multiple method signatures followed by an implementation that fulfills all variations. To correctly differentiate parameters, type guards or instanceof are usually applied.
Example code:
class MyLogger { log(message: string): void; log(message: string, level: 'info' | 'error'): void; log(message: string, level?: 'info' | 'error'): void { const lvl = level ?? 'info'; console.log(`[${lvl}] ${message}`); } }
Key features:
Is it possible to implement two implementations of the same method with different sets of parameters?
No. In TypeScript, only one implementation is allowed. Multiple methods with the same name are a syntax error.
How to type rest parameters when overloading methods to avoid losing strict typing?
It is recommended to describe exact parameters in signatures, and in the implementation — the most generalized:
class Test { doWork(a: number): void; doWork(a: string): void; doWork(a: number | string): void { //... } }
What will happen if the return type of overloaded signatures is different?
TypeScript will require that the implementation returns a union type. Otherwise, a compilation error occurs.
class X { get(value: number): string; get(value: string): number; get(value: number | string): string | number { return typeof value === 'number' ? 'number' : 42; } }
In the product, there was an attempt to implement two methods with the same name for different parameter types in a class. After compilation, the method "replaced" the last declaration, all other versions were ignored, leading to bugs.
Advantages:
Disadvantages:
Multiple signatures with union-type parameters were made, and all variations were handled in the method through type guards. The compiler immediately warned about type issues.
Advantages:
Disadvantages: