You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionconsumer(valueProducer: (this: void)=>number){console.log(valueProducer());}classFoo{private_value: number=100;// this method requires `this: this` or `this: Foo` to provide type safetygetValue(): number{returnthis._value;}constructor(){// callback does not have `this: void`, but Typescript can't warn meconsumer(this.getValue);}}
Expected behavior:
Class methods default to this: this.
Actual behavior:
Class methods default to this: any?
I'm not familiar with Git so I'm not good at distinguishing discussions vs actually implemented code, but #6739 appears to address the problem directly and includes the line:
"if --strictThis is specified, then functions will default this: void and methods will default this: this."
Which is exactly what I want. However, that page has an edit saying that the feature was "removed from this release". That was over a year ago. Was a fix reintroduced somewhere else? Catching this error seems like exactly the thing you'd want a type system for.
The text was updated successfully, but these errors were encountered:
Unfortunately the perf cost of automatically putting the this type in class methods was extremely high (30% or worse degradation in all scenarios involving classes) and we weren't able to find a solution that had acceptable performance trade-offs.
Aluanhaddad, if you instantiate foo, you'll log "undefined".
Read the comments in the supplied example code, and the expected vs actual behavior.
I want typescript to recognize that the method passed to consumer will be called with the wrong this. I can achieve this by explicitly typing this in the method, e.g. (getValue(this: this): number), but that requires annotating every class method in my codebase with something Typescript seemingly should already know. #6739 addresses this directly, but it looks like the crucial part was pulled at the last minute. This was over a year ago, and there are other proposals about this issue referenced there. I was (Ryan answer the question) trying to figure out if this was ever actually addressed and how I can enable it.
we weren't able to find a solution that had acceptable performance trade-offs
TypeScript Version: 2.2.1
Code
Expected behavior:
Class methods default to
this: this
.Actual behavior:
Class methods default to
this: any
?I'm not familiar with Git so I'm not good at distinguishing discussions vs actually implemented code, but #6739 appears to address the problem directly and includes the line:
Which is exactly what I want. However, that page has an edit saying that the feature was "removed from this release". That was over a year ago. Was a fix reintroduced somewhere else? Catching this error seems like exactly the thing you'd want a type system for.
The text was updated successfully, but these errors were encountered: