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
functionassert(expr: boolean,msg: string=""){if(!expr){console.log("assertion failed!");throwmsg;}}classFoo{_bar: Bar;constructor(){this._bar=newBar();this._bar.Callback=this.callback;}publicrun(){this._bar.Callback();}callback(){assert(!(thisinstanceofBar),"\"this\" has unexpected type \"Bar\"");assert(thisinstanceofFoo,"\"this\" has unexpected type!");console.log("Test passed, \"this\" retains the expected type.");}}classBar{publicCallback: ()=>void;publicrun(){this.Callback();}}varf=newFoo();f.run();
I realize the current behavior is by-design, this is a suggestion/request-for-comment to improve type safety here.
Expected behavior:
The code above (playground link) was written with the (incorrect/naive) assumption that at line 13 (this._bar.Callback = this.callback;) the function reference would be stored bound to the value of this at the time of execution of that line.
I understand why TypeScript can't ditch this horrible behavior and evaluate the code sanely for reasons of backwards compatibility when changing JS file extensions to .TS, but as this is a common type-safety issue, I feel like TS should take measures to mitigate the issue.
TypeScript should be able to catch the unsafe usage of this in the example above (it fails the assertions).
Additionally, when typing this.<TAB>, the suggestions generated are for values based on the current scope instead of being any or something else, leading one to believe the code is correct.
Suggestions:
Perhaps a --strictThis option generating an error when this.xxx is assigned to an object of a different type from the type of this in the current scope?
A new keyword, e.g. self, the value of which binds to the current value of this that can be used instead.
Actual behavior:
The line this._bar.Callback = this.callback; does not resolve the value of this at the time it is invoked, leading to the failure of the assertion.
TypeScript Version: 2.8.3
Search Terms: this, intitle:this, this bind
I realize the current behavior is by-design, this is a suggestion/request-for-comment to improve type safety here.
Expected behavior:
The code above (playground link) was written with the (incorrect/naive) assumption that at line 13 (
this._bar.Callback = this.callback;
) the function reference would be stored bound to the value ofthis
at the time of execution of that line.I understand why TypeScript can't ditch this horrible behavior and evaluate the code sanely for reasons of backwards compatibility when changing JS file extensions to .TS, but as this is a common type-safety issue, I feel like TS should take measures to mitigate the issue.
TypeScript should be able to catch the unsafe usage of
this
in the example above (it fails the assertions).Additionally, when typing
this.<TAB>
, the suggestions generated are for values based on the current scope instead of beingany
or something else, leading one to believe the code is correct.Suggestions:
--strictThis
option generating an error whenthis.xxx
is assigned to an object of a different type from the type ofthis
in the current scope?self
, the value of which binds to the current value ofthis
that can be used instead.Actual behavior:
The line
this._bar.Callback = this.callback;
does not resolve the value ofthis
at the time it is invoked, leading to the failure of the assertion.Playground Link:
Link
Related Issues:
The text was updated successfully, but these errors were encountered: