যখন আমি সক্ষম 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।
