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

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

Closed
ktsn opened this issue Dec 9, 2018 · 7 comments
Closed

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

ktsn opened this issue Dec 9, 2018 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@ktsn
Copy link

ktsn commented Dec 9, 2018

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.

@HerringtonDarkholme
Copy link
Contributor

HerringtonDarkholme commented Dec 9, 2018

@fenying
Copy link

fenying commented Dec 9, 2018

Maybe it same as this issue #26206 (comment)

@DanielRosenwasser
Copy link
Member

I believe what you really want is #5863.

@DanielRosenwasser
Copy link
Member

I would suggest patching over vuejs/vue#8595 to return VueConstructor<V> instead of this.

@DanielRosenwasser
Copy link
Member

...like in vuejs/vue#9173 😄

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Dec 10, 2018
@ktsn
Copy link
Author

ktsn commented Dec 10, 2018

Oh, I didn't realize #5863 is related. Thank you for pointing it out and suggestion!
Yes, it looks better to patch Vue side 🙂

@ktsn ktsn closed this as completed Dec 10, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed automatic 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

5 participants