2014-12-03 2 views
0

입력 된 스크립트에서 메소드가 어떻게 오버로드됩니까? 다음 코드를 감안할 때 인터페이스를 구현하는 클래스가 있습니다. 인터페이스 'polymorhpic'메서드가 있지만 그들을 구현할 것 - 오류 "중복 식별자 '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; }  

} 
+0

내가 거기 아시다시피 타이프 스크립트에서는 그렇게 할 수 없습니다. 선택적 매개 변수 'MyMethod (param : string = "")를 사용하여 단일 메서드를 만들 수 있습니다. 문자열 { if (param == "") \t return 1; else return "hello world"; }' –

+0

예, 문제는 인터페이스를 사용하지 못하게하는 것입니다. – MarzSocks

답변

1

OK, 당신은 이런 식으로 그것을 할, (MyMethod라는의 실제 구현은 모든 입력을 다루고 있습니다 및 형태를 돌려주는) :

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

} 
관련 문제