How to implement interface overloading

Anders gives this example of an interface overload, but I don’t understand how it will 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?

+3
source share
1 answer
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?");           
        }
    }
}
+3
source

All Articles