যখন আমি সক্ষম 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
কলব্যাক পরামিতিগুলিতে টাইপ করা একই ত্রুটির ফলাফল:
foo.on('error', (this: Foo, err: any) => { // error: `this` implicitly has type `any`
একটি workaround this
অবজেক্টের সাথে প্রতিস্থাপন করা হয়:
foo.on('error', (err: any) => {
console.log(err);
foo.emit('end');
});
তবে এই ত্রুটির সঠিক সমাধান কী?
আপডেট: এটি this
কলব্যাকে একটি টাইপ যুক্ত প্রকৃতপক্ষে ত্রুটির ঠিকানা দেখায় । আমি ত্রুটিটি দেখছিলাম কারণ আমি একটি ধরণের টীকাযুক্ত সহ একটি তীর ফাংশনটি ব্যবহার করছিলাম this
:
this
।