नहीं है, मैं निम्नलिखित कोड के लिए यह त्रुटि प्राप्त:'इस' परोक्ष प्रकार है 'कोई भी' जब मैं <code>tsconfig.json</code> में <code>noImplicitThis</code> सक्षम है क्योंकि यह एक प्रकार एनोटेशन
'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
कॉलबैक करने के लिए वास्तव में त्रुटि के पते जोड़ने पता चला है।
क्या आपने इसे टाइपस्क्रिप्ट 2.1 या रात के संस्करण पर आजमाया था? –
@DanielRosenwasser 2.1.4 – tony19
और अब मुझे वेबस्टॉर्म और टीएस खेल का मैदान शिकायत करने का कारण दिखाई देता है: मैं 'इस' के लिए एक प्रकार की एनोटेशन प्रदान करते समय एक तीर फ़ंक्शन का उपयोग कर रहा था। – tony19