Skip to content

this type in constructor interface will not be extended in sub class #28930

Closed
@ktsn

Description

@ktsn

TypeScript Version: 3.3.0-dev.20181208

Search Terms: constructor interface this extends sub class

Code

interface AConstructor {
    new (): AInstance
    a(): this
}

interface AInstance {
    a: string
}

declare const A: AConstructor 

declare function test<T extends typeof A>(a: T): T

class B extends A {
    b: string
}

const B1: typeof B = B.a() // Unexpected: Return type is AConstructor
const B2: typeof B = test(B) // Unexpected: Type parameter is inferred to AConstructor

Expected behavior:
The return type of B.a() and test(B) is typeof B.

Actual behavior:
The return type of B.a() and test(B) is AConstructor.

Playground Link:
https://www.typescriptlang.org/play/#src=interface%20AConstructor%20%7B%0D%0A%20%20%20%20new%20()%3A%20AInstance%0D%0A%20%20%20%20a()%3A%20this%0D%0A%7D%0D%0A%0D%0Ainterface%20AInstance%20%7B%0D%0A%20%20%20%20a%3A%20string%0D%0A%7D%0D%0A%0D%0Adeclare%20const%20A%3A%20AConstructor%20%0D%0A%0D%0Adeclare%20function%20test%3CT%20extends%20typeof%20A%3E(a%3A%20T)%3A%20T%0D%0A%0D%0Aclass%20B%20extends%20A%20%7B%0D%0A%20%20%20%20b%3A%20string%0D%0A%7D%0D%0A%0D%0Aconst%20B1%3A%20typeof%20B%20%3D%20B.a()%20%2F%2F%20Unexpected%3A%20Return%20type%20is%20AConstructor%0D%0Aconst%20B2%3A%20typeof%20B%20%3D%20test(B)%20%2F%2F%20Unexpected%3A%20Type%20parameter%20is%20inferred%20to%20AConstructor

Related Issues:
vuejs/vue-class-component#294

Background: I've found this behaviour while investigating the above issue. This is caused by recent changes for Vue's type declaration. It looks like the extending class does not match with the decorator type because this type is still base constructor type.

I'm still not sure if my guess is correct. If it is not true, please just let me know about that.

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