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

Support for new.target #8494

Closed
kitsonk opened this issue May 6, 2016 · 4 comments
Closed

Support for new.target #8494

kitsonk opened this issue May 6, 2016 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@kitsonk
Copy link
Contributor

kitsonk commented May 6, 2016

TypeScript Version:

1.8.10

Code

function Foo() {
  console.log(new.target);
}

new Foo();

Expected behavior:

When targeting ES6, this should compile without an error.

new.target is an obscure bit of ES6, but it is there, for example running the code in an ES6 browser returns what is expected in that new.target === Foo. When it is just Foo() without new then new.target === undefined.

While it maybe challenging to come up with an emit that works for ES5, it should be at least supported when targeting ES6.

I suspect and emit for ES6 might be something like this:

function Foo() {
  var _newTarget = this === window ? undefined : this && this.constructor;
  console.log(_newTarget);
}

Actual behavior:

Compiles as:

$ tsc --target es6 test.ts
test.ts(2,18): error TS1109: Expression expected.
@basarat
Copy link
Contributor

basarat commented May 6, 2016

I think it should just be supported valid compile without getting into how to do a great emit.

That said window would be a bad idea (e.g. nodejs)

@kitsonk
Copy link
Contributor Author

kitsonk commented May 6, 2016

That said window would be a bad idea (e.g. nodejs)

Revised:

function Foo() {
  var _newTarget = this === (typeof window === 'undefined' ? global : window) ? undefined : this && this.constructor;
  console.log(_newTarget);
}

Foo();

When 'use strict'; is used, then this will be undefined anyways and the emit could simply be:

'use strict';

function Foo() {
  var _newTarget = this && this.constructor;
  console.log(_newTarget);
}

Foo();

@mhegazy
Copy link
Contributor

mhegazy commented May 6, 2016

duplicate of #2551

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 6, 2016
@mhegazy mhegazy closed this as completed May 6, 2016
@kitsonk
Copy link
Contributor Author

kitsonk commented May 6, 2016

Ooops... sorry... I will move over to there...

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
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

3 participants