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

Parent classes ignore the signature of overridden methods #52113

Closed
jfparadis-appomni opened this issue Jan 5, 2023 · 2 comments
Closed

Parent classes ignore the signature of overridden methods #52113

jfparadis-appomni opened this issue Jan 5, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jfparadis-appomni
Copy link

jfparadis-appomni commented Jan 5, 2023

Bug Report

When a subclass overrides a method called by its parent class, the signature of the overridden method is ignored by the parent class. This is also the case with static classes.

In our example:

  • A function, type, returns a constructor, X.
  • Another method, make, invokes it and returns a constructed object, new X.
  • A subclass overrides the first function, type, and returns a different constructor, Y.
  • At runtime, calling the method make will create an instance of that second constructor, a new Y.
  • Typescript reports that the instance type should still be X.

🔎 Search Terms

subclass overrides static method called parent class

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about override

⏯ Playground Link

Playground link with relevant code

Playground link with relevant code (static)

💻 Code

class X {}
class Y extends X {}

class A {
  static make() {
    const constructor = this.type();
    return new constructor()
  }

  static type() {
    return X;
  }
}

class B extends A {
  static type() {
    return Y;
  }
}

const a = A.make(); // type of a is X
const b = B.make(); // type of b is X <== incorrect

console.log(a.constructor.name); // outputs "X"
console.log(b.constructor.name); // outputs "Y"

🙁 Actual behavior

The type set by the overridden function is lost.

🙂 Expected behavior

Same as JS, the overridden function sets the type.

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jan 5, 2023

See #5863 for the static case

For the nonstatic case, a type annotation is necessary

  make(): ReturnType<this["type"]> {

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 5, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

No branches or pull requests

3 participants