10

i am new to typescript, here is a interface which i'd like to implement;

interface IThing{
    name:string;
    age:number;
    sayHello:{
        (name:string):string;
        (age:number):number;
    }
}

sayHello has two signatures which indicates overload version. i just don't know how to implement that in a class, any help? thanks.

Daryl
  • 18,592
  • 9
  • 78
  • 145
paul cheung
  • 748
  • 2
  • 13
  • 32
  • That doesn't look like valid TypeScript to me. Have you had a look at the code samples here? http://www.typescriptlang.org/Tutorial/ – Robert Harvey Jul 31 '13 at 03:01
  • there is no errors occurs via IntelSense, i get this code snippet from the typescript introduction video here: http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript – paul cheung Jul 31 '13 at 03:06
  • 4
    This is perfectly valid TypeScript and this is a valid, straightforward question. Vote to reopen. – Ryan Cavanaugh Jul 31 '13 at 05:11
  • What you have defined in your interface is a `sayHello` object with two different constructors. This is why it is valid TypeScript - it just isn't the TypeScript you are looking for. See the answer from @RyanCavanaugh for overload syntax. – Fenton Jul 31 '13 at 07:43

1 Answers1

16

To implement an overloaded function, write all the overload call signatures you want to be visible first, followed by an implementation signature that is a superset of all the overload signatures. Example:

class Thing implements IThing {
    // Implement the name and age fields
    name: string;
    age: number;

    // Overload signature #1
    sayHello(name: string): string;
    // Overload signature #2
    sayHello(age: number): number;
    // Implementation signature, not visible to external callers
    sayHello(arg: any): any {
        if(typeof arg === 'string') {
            return this.name;
        } else if(typeof arg === 'number') {
            return this.age;
        } else {
            throw new Error('sayHello can only take string or a number');
        }
    }
}

var x = new Thing();
var n = x.sayHello('world'); // n: string
var a = x.sayHello(42); // a: number
var e = x.sayHello(false); // error: false is not string or number
Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
  • +1. I would add that you leave out the implementation signature when adding an overloaded method to an interface, you only need that for classes, as shown in this answer. – Fenton Jul 31 '13 at 07:41
  • this works fine. thanks, is this the only way to do this implementation? i find this overrided mechanism seems to be just used to restrict the type of input-args/return-values? if not i could declare the function signature as sayHello(arg:any):any in my interface, and then stuff the code snippet sayHello(arg:any):any{ //if...else...} in my implementation class. any help clear my confusion? – paul cheung Jul 31 '13 at 07:43
  • @Steve Fenton, sorry i don't understand what you say well, only need that for classes? – paul cheung Jul 31 '13 at 08:51
  • @paulcheung Yes - the implementation signature (the one accepting an argument of type `any` above) is only needed on a class. The other two (accepting arguments of types `string` and `number` above) are the only ones you need to add on an interface. – Fenton Jul 31 '13 at 12:40
  • @SteveFenton very kind of you! – paul cheung Aug 01 '13 at 02:31