noImplicitThis
에서 활성화 tsconfig.json
하면 다음 코드에 대해이 오류가 발생합니다.
'this' implicitly has type 'any' because it does not have a type annotation.
class Foo implements EventEmitter {
on(name: string, fn: Function) { }
emit(name: string) { }
}
const foo = new Foo();
foo.on('error', function(err: any) {
console.log(err);
this.emit('end'); // error: `this` implicitly has type `any`
});
this
콜백 매개 변수에 typed 를 추가 하면 동일한 오류가 발생합니다.
foo.on('error', (this: Foo, err: any) => { // error: `this` implicitly has type `any`
해결 방법은 this
객체 로 바꾸는 것 입니다.
foo.on('error', (err: any) => {
console.log(err);
foo.emit('end');
});
그러나이 오류에 대한 적절한 수정은 무엇입니까?
업데이트 :this
콜백에 유형 을 추가하면 실제로 오류가 해결됩니다. 다음에 대한 유형 주석과 함께 화살표 함수를 사용했기 때문에 오류가 발생했습니다 this
.
TypeScript 2.1 또는 야간 버전에서 사용해 보셨습니까?
—
Daniel Rosenwasser
@DanielRosenwasser 2.1.4
—
tony19
이제 WebStorm과 TS 플레이 그라운드가 불평하는 이유를 알 수 있습니다. .NET에 유형 주석을 제공하는 동안 화살표 함수를 사용하고있었습니다
—
tony19
this
.
여기에 버그를 신고했습니다 : github.com/Microsoft/TypeScript/issues/13768- 자유롭게 추적하고 좋아요를 눌러 주세요 .
—
Daniel Rosenwasser