Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of this mismatches #23749

Closed
mqudsi opened this issue Apr 27, 2018 · 2 comments
Closed

Better handling of this mismatches #23749

mqudsi opened this issue Apr 27, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@mqudsi
Copy link

mqudsi commented Apr 27, 2018

TypeScript Version: 2.8.3

Search Terms: this, intitle:this, this bind

function assert(expr: boolean, msg: string = "") {
	if (!expr) {
		console.log("assertion failed!");
		throw msg;
	}
}

class Foo {
	_bar: Bar;

	constructor() {
		this._bar = new Bar();
		this._bar.Callback = this.callback;
	}

	public run() {
		this._bar.Callback();
	}

	callback() {
		assert(!(this instanceof Bar), "\"this\" has unexpected type \"Bar\"");
		assert(this instanceof Foo, "\"this\" has unexpected type!");
		console.log("Test passed, \"this\" retains the expected type.");
	}
}

class Bar {
	public Callback: () => void;
	public run() {
		this.Callback();
	}
}

var f = new Foo();
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.

Playground Link:
Link

Related Issues:

@mhegazy
Copy link
Contributor

mhegazy commented Apr 27, 2018

duplicate of #10288

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 27, 2018
@mqudsi
Copy link
Author

mqudsi commented Apr 28, 2018

Thanks, Mohamed! If it's OK with you, I'll copy my comments there.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants