When you overload in TypeScript, you only have one implementation with multiple signatures.
class Foo {
myMethod(a: string);
myMethod(a: number);
myMethod(a: number, b: string);
myMethod(a: any, b?: string) {
alert(a.toString());
}
}
Only the three overloads are recognized by TypeScript as possible signatures for a method call, not the actual implementation.
In your case, I would personally use two methods with different names as there isn't enough commonality in the parameters, which makes it likely the method body will need to have lots of "ifs" to decide what to do.
As of TypeScript 1.4, you can typically remove the need for an overload using a union type. The above example can be better expressed using:
myMethod(a: string | number, b?: string) {
alert(a.toString());
}
The type of a
is "either string
or number
".