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; }     

}
MarzSocks
  • 4,229
  • 3
  • 22
  • 35
  • 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"; }` – Raziza O Dec 03 '14 at 12:57
  • yeah the problem is the interface wont let you do this – MarzSocks Dec 03 '14 at 13:08

1 Answers1

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"; }     

}
MarzSocks
  • 4,229
  • 3
  • 22
  • 35