Ben etkinleştirdiğinizde noImplicitThis
içinde tsconfig.json
, ben şu kodu bu hatayı alıyorum:
'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
Geri arama parametrelerine bir typed eklemek aynı hataya neden olur:
foo.on('error', (this: Foo, err: any) => { // error: `this` implicitly has type `any`
Geçici bir çözüm, this
nesneyi değiştirmektir :
foo.on('error', (err: any) => {
console.log(err);
foo.emit('end');
});
Ancak bu hata için doğru düzeltme nedir?
GÜNCELLEME: Geri aramaya bir yazılanın eklenmesi this
gerçekten hatayı giderir. Hatayı görüyordum çünkü aşağıdakiler için tür ek açıklamasına sahip bir ok işlevi kullanıyordum this
:
this
.