0
노드를 확장하는 클래스가 있다고 가정하십시오. EventEmitter
. 하지만 등록 할 수있는 이벤트를 제한하고 싶습니다.typescript에서 상속 된 메서드의 매개 변수를 제한 할 수 있습니까?
class Foo extends EventEmitter {
on(event: 'myEvent', listener: Function): this;
emit(event: 'myEvent', ...args: any[]): boolean
}
var foo = new Foo();
foo.on('wrongEvent',()=>{}); // this should cause compiling error
달성 가능합니까?
t.ts(6,3): error TS2391: Function implementation is missing or not immediately following the declaration.
t.ts(7,3): error TS2391: Function implementation is missing or not immediately following the declaration.
t.ts(10,8): error TS2345: Argument of type '"wrongEvent"' is not assignable to parameter of type '"myEvent"'.
가 어떻게이 TS2391 오류를 방지 할 수 있습니다 :
나는 오류를 받고 있어요?
TS2391 단지 구현이 없음을 말한다 코멘트.의 자신 @ AlekseyL에 따라이 대답, 그래서 그것을 구현하는 것입니다. –
한계 매개 변수 유형 - [문자열 리터럴 유형] (https://www.typescriptlang.org/docs/handbook/advanced-types.html#string-literal-types) –
@AlekseyL을 시도 할 수 있습니다. 감사! 이 경우 상위 클래스에서 구현을 사용하고 싶습니다. 그것은 가능합니까? – zjk