Using Typescript 3.8.3 and node v12.16.2:
class dummy{
public data = '12345';
public somePromise(d : dummy){
var e : dummy = this;
return new Promise(function(resolve, reject) {
console.log(d.data); //This is fine.
console.log(e.data); //This is fine too.
//However this, pun intended, is not fine:
console.log(this.data); //TypeError: Cannot read property 'data' of undefined
resolve ('promise');
})
}
public async run(){
console.log(this.data); //This is off course fine.
await this.somePromise(this);
}
}
var d = new dummy();
d.run();
Can someone please point me to some documentation that explains what is going on and why? Second question is how can I make visual studio code or the tsc compiler flag the error?
The answer is good. For details read here. And to make Visual studio flag the error use --noImplicitThis in tsconfig.json.