Skip to content

Wrong type inference, returns the base class type instead of the child class type #57286

Description

🔎 Search Terms

None

🕗 Version & Regression Information

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

⏯ Playground Link

Playground Link

💻 Code

// BUG1:{classBase{getself(): Base{returnthis}}classChildextendsBase{overridegetself(): Child{returnthis}child=true;}functionconvert<TextendsBase>(X: T){// implicitly returns Base["self"] instead of T["self"]returnX.self;}constvalue=convert(newChild());// expected: typeof value = Child.// got: typeof value = Base.functionconvert2<TextendsBase>(X: T): T["self"]{returnX.self;}constvalue2=convert2(newChild());// typeof value = Child.}// BUG2:{classBaseClass<V>{protectedfake(): V{thrownewError("")}}classKlass<V>extendsBaseClass<V>{child=true;}typeConstructor<PextendsBaseClass<number>>=new()=>P;typeinferTest<T>=TextendsConstructor<infer P> ? P :never;typeU=inferTest<Constructor<Klass<number>>>// U = Klass<number>}{classBaseClass<V>{protectedfake(): V{thrownewError("")}}classKlass<V>extendsBaseClass<V>{child=true;}typeConstructor<V,PextendsBaseClass<V>>=new()=>P;typeinferTest<V,T>=TextendsConstructor<V, infer P> ? P :never;typeU=inferTest<number,Constructor<number,Klass<number>>>// got: U = BaseClass<number>//expected: U = Klass<number>}

🙁 Actual behavior

Bug1: It seems that when calling methods in generic function foo<T extends Base>(t: T) :

t.foo()// viewed as method Base.foo()// instead of being viewed as T.foo() with T extends Base.

Bug2: For type inference, it seems that adding a generic parameters confuses TS.

🙂 Expected behavior

Cf code.

Additional information about the issue

Related to issue #57102

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions