जब मैं सक्षम 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`
this
ऑब्जेक्ट के साथ प्रतिस्थापित करने के लिए एक वर्कअराउंड है :
foo.on('error', (err: any) => {
console.log(err);
foo.emit('end');
});
लेकिन इस त्रुटि के लिए उचित निर्धारण क्या है?
अद्यतन: यह this
कॉलबैक में टाइप किए गए को जोड़ता है जो वास्तव में त्रुटि को संबोधित करता है। मैं त्रुटि देख रहा था क्योंकि मैं एक एनोटेशन के साथ एक तीर फ़ंक्शन का उपयोग कर रहा था this
:
this
।