Greg Gum
Greg Gum

Reputation: 38149

How to implement interface overload

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

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

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

Related Questions