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
With structural typing, generics, union, conditional types, etc. Typescript has reached the heaven of sofistication, but one (the?) elephant in the room is the annoying behaviour of 'this' in typescript.
I know that TS has a way to define the type of the this parameter, but does nothing in checking whether this is going to be bound or not by the time the function runs.
Every class method (not lambda) that uses this (directly or in a inner lambda) is internally marked in the compiler as 'unbound-this'.
Methods marked with unbout this can be only called as an instance method, and can not be assigned to variables or passed as parameters.
This mark can be reverted with some cast or using bind.
<buttononClick={this.handleMethod}/>//TS Error: this will be undefined. <buttononClick={this.handleMethodas(()=>void)}>//Avoid TS error at your own risk.
I think lambda capturing is considered:
<buttononClick={()=>this.handleMethod()}/>
In this case, the lamda is already capturing this, and we are just calling the method not passing it, so should be ok.
Limitations
When this is used in normal functions outside of classes, (where you need the explicit this parameter in strict) this tracking will not help (for now).
functionjoinComma(thisany[]){returnthis.join(", ");}Array.prototype.joinComma=joinComma;[1,2,3].joinComma();//OkjoinComma([1,2,3]);//Runtime error, still not solved in TS.
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
It will add aditional errors, but that's the 'name of the game'.
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. new expression-level syntax)
The text was updated successfully, but these errors were encountered:
--noImplicitThis handles a set of these scenarios. there are set that were not handled because of implementation issues. the remaining scenarios are tracked by #10288
I'm working with strict for a long time, and I'm sure TS is not caching this issue. Is not about the type of this. Is about making sure this will be bound by the time the method is called.
This should be forbidden:
classPerson{name: string;greeting(){console.log(this.name);}}varp=newPerson();p.name="John";vargr=p.greeting;// Error, you can not get a naked fointer to a funtion that requires this, without a casting
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Search Terms
undefined this
Suggestion
With structural typing, generics, union, conditional types, etc. Typescript has reached the heaven of sofistication, but one (the?) elephant in the room is the annoying behaviour of 'this' in typescript.
https://twitter.com/bendhalpern/status/578925947245633536
I know that TS has a way to define the type of the
this
parameter, but does nothing in checking whetherthis
is going to be bound or not by the time the function runs.The problem
The solution
Every class method (not lambda) that uses
this
(directly or in a inner lambda) is internally marked in the compiler as 'unbound-this'.Methods marked with unbout this can be only called as an instance method, and can not be assigned to variables or passed as parameters.
This mark can be reverted with some cast or using
bind
.I think lambda capturing is considered:
In this case, the lamda is already capturing
this
, and we are just calling the method not passing it, so should be ok.Limitations
When
this
is used in normal functions outside of classes, (where you need the explicitthis
parameter instrict
) this tracking will not help (for now).Checklist
My suggestion meets these guidelines:
It will add aditional errors, but that's the 'name of the game'.
The text was updated successfully, but these errors were encountered: