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.

4
  • That doesn't look like valid TypeScript to me. Have you had a look at the code samples here? typescriptlang.org/Tutorial Commented Jul 31, 2013 at 3:01
  • there is no errors occurs via IntelSense, i get this code snippet from the typescript introduction video here: channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript Commented Jul 31, 2013 at 3:06
  • 4
    This is perfectly valid TypeScript and this is a valid, straightforward question. Vote to reopen. Commented Jul 31, 2013 at 5: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. Commented Jul 31, 2013 at 7:43

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

+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.
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?
@Steve Fenton, sorry i don't understand what you say well, only need that for classes?
@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.
@SteveFenton very kind of you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.