-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
No way to get generic type of an abstract class inside a factory function #35576
Comments
I have to add that if in classFactory2 the return statement is changed to
Then oddly enough the "InstanceViaNew" works, yet "InstanceViaProto" still doesn't (I'd have expected the opposite) |
|
@rbuckton I think the PR will certainly make InstanceViaNew work (nice!), but I guess it wouldn't fix the InstanceViaProto case? |
Also I found out that if you do something like this: function classFactory2<T>() {
abstract class ACS {
constructor(_y: number) {}
x!: T
}
return ACS
}
const ACS = classFactory2<string>()
// this doesn't work because the abstract has no constructor (expected I guess)
type AC3 = InstanceType<typeof ACS>["x"] // Type 'typeof ACS' does not satisfy the constraint 'new (...args: any) => any'
type AC4 = ConstructorParameters<typeof ACS> // Type 'typeof ACS' does not satisfy the constraint 'new (...args: any) => any'.
// but the intersection of the type with anything makes it somehow work... ?
// as if the intersection made the class get "unabstracted"
type AC1 = InstanceType<(typeof ACS) & {whatever: any}>["x"] // weirdly enough this gives string
type AC2 = ConstructorParameters<(typeof ACS) & {whatever: any}> // weirdly enough this gives [number] |
TypeScript Version: 3.7.3
Search Terms: abstract class, generic, infer
Code
Expected behavior:
There should be a way to make abstract classes keep the generic information and use it.
Actual behavior:
There's no way to make abstract classes keep the generic information and use it (unless they are type-casted to non abstract classes by adding to them a fake constructor).
Related Issues: #26829
The text was updated successfully, but these errors were encountered: