Skip to content

new.target is not inferred as a "this" type #13849

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

Closed
justinfagnani opened this issue Feb 2, 2017 · 1 comment
Closed

new.target is not inferred as a "this" type #13849

justinfagnani opened this issue Feb 2, 2017 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@justinfagnani
Copy link

TypeScript Version: [email protected]

Code

class A {
  a: this;

  constructor() {
    super();
    this.a = Object.create(new.target.prototype); // [ts] Type 'A' is not assignable to type 'this'.
    this.a = new (new.target)(); // [ts] Type 'A' is not assignable to type 'this'.
  }
}

The assignment should be allowed in both cases.

Expected behavior:

No errors

Actual behavior:

Errors

@rbuckton
Copy link
Member

rbuckton commented Feb 8, 2017

We don't currently expose a this type for the static side of a class, and we do not currently expose the prototype of the static side of a class as a this type. Until such time as we decide to change that behavior, this is functioning as intended.

Please note that you can avoid the error by introducing a type assertion in both cases:

class A {
  a: this;

  constructor() {
    this.a = Object.create(<this>new.target.prototype);
    this.a = <this>new (new.target)();
  }
}

@rbuckton rbuckton closed this as completed Feb 8, 2017
@rbuckton rbuckton added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 8, 2017
@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
Bug A bug in TypeScript Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants