0

How does one overload methods in typed script. Given the following code, I have a class which implements an interface. The interface has 'polymorhpic' methods but I cant seem to implement them - getting the error "duplicate identifyer 'MyMethod'".

export class IService {

   MyMethod(): string;
   MyMethod(value: string): number;

}

export class MyService implements IService {

   MyMethod(): string { return "hello world;" } 
   MyMethod(value: string): number { return 1; }     

}
2
  • As i know it there is not possible to do that in typescript. You can create a single method with optional parameter MyMethod(param: string = ""): string { if (param == "") return 1; else return "hello world"; } Commented Dec 3, 2014 at 12:57
  • yeah the problem is the interface wont let you do this Commented Dec 3, 2014 at 13:08

1 Answer 1

1

OK I've managed to solve the problem, you do it like this, (note that the actual implementation of MyMethod covers all input and return types):

export class IService {

   MyMethod(): string;
   MyMethod(value: string): number;

}

export class MyService implements IService {

   MyMethod(): string;
   MyMethod(value: string): number;
   MyMethod(value: string = ""): any { if(value != "") return 1 else return "hello world"; }     

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.