Skip to content

Wrong type of class instance if the constructor returns different class instance #50126

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
Tomas2D opened this issue Aug 1, 2022 · 2 comments

Comments

@Tomas2D
Copy link

Tomas2D commented Aug 1, 2022

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class A {
    greet() {}
}

class C {
  constructor() {
      return new A();
  }
}

let instance: A = new C() // error

πŸ™ Actual behavior

Property 'greet' is missing in type 'C' but required in type 'A'.(2741)

πŸ™‚ Expected behavior

I think that compiler should infer that the constructor will return an instance of a different class

@MartinJohns
Copy link
Contributor

MartinJohns commented Aug 1, 2022

Duplicate of #27594 (IMO). Currently constructors can't have a different return type, and that's the issue to solve it.

Related: #13819

@Tomas2D
Copy link
Author

Tomas2D commented Aug 2, 2022

Sorry for that, you are right. It is a duplicate. Closing for now.

@Tomas2D Tomas2D closed this as completed Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants