인터페이스를 구현할 때 TypeScript에서 함수의 반환 형식을 특정 형식의 'any'형식으로 오버로드 할 수있는 이유를 결정하려고합니다.인터페이스를 구현할 때 TypeScript가 'any'로 반환 형식의 오버로드를 허용하는 이유
내 경우 엔 Angular로 작업 중이며 구현 된 클래스를 주입하고 있습니다.
내 환경 :
비주얼 스튜디오 2017
각도 버전 1.5.5
타이프 라이터 버전 다음 코드는 어떤 문제없이 컴파일 2.1.5
:
export interface IFoo {
thing: (parameter: number) => string;
}
export class BarService implements IFoo {
public thing = (parameter: number): any => {
return { "whatever": parameter };
}
}
angular.module("FooBar").service("barService", BarService);
그래서 IFoo inter를 소비하려고 할 때 얼굴과 '일'함수 호출에서 반환되는 문자열을 기대하고 컴파일러는 실제로 그것이 일어날 수 있습니다!
export class Whatever {
public foo: IFoo;
public myString: string;
static $inject = ["barService"];
constructor(barService: IFoo) {
this.foo = barService;
this.myString = this.foo.thing(0);
}
}
그것은 반환 형식이 형식에 과부하시 타이프 라이터 컴파일 실패 할 것 같다 '어떤'인터페이스의 소비자는 강력한 형식의 객체를 기대하고 있기 때문이다.
'any'의 전체 점이 절대 유형 오류의 원인이되지 않아야합니다 –