Skip to content

Polymorphic this with subclassing #17533

@cletusw

Description

@cletusw

Using Polymorphic "this" for static members workaround from #5863 no longer works with subclassing in TypeScript 2.4.

TypeScript Version: 2.4.2

Code

class Parent {
    static create<T extends Parent>(this: { new (): T }): T {
        return new this();
    }
}

class Child extends Parent {
    static create(): Child {
        return super.create<Child>();
    }
}

Expected behavior:
Should not have type errors, which was the behavior in 2.3.4.

Actual behavior:

test.ts(7,7): error TS2417: Class static side 'typeof Child' incorrectly extends base class static side 'typeof Parent'.
  Types of property 'create' are incompatible.
    Type '() => Child' is not assignable to type '<T extends Parent>(this: new () => T) => T'.
      Type 'Child' is not assignable to type 'T'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions