Reputation: 38149
Anders give this example of an interface overload, but I don't get how it would be implemented and used:
interface Fooable
{
Foo(n:number) : string;
Foo(s:string) : string;
}
Can someone give me an example of implementing and using this?
Upvotes: 4
Views: 1678
Reputation: 221402
class Bar implements Fooable {
Foo(n: number): string;
Foo(n: string): string;
Foo(n: any) {
if(typeof n === 'number') {
return n + ' is a number';
} else if(typeof n === 'string') {
return n + ' is a string';
} else {
throw new Error("Oops, null/undefined?");
}
}
}
Upvotes: 4