-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Generics and extending an abstract class #25606
Comments
To get a mixin to work properly, you need to use a generic type for the class that extends function extend<P, R, Cls extends { new(...args:any[]): any }>(Component: Cls, getSomething: GetSomething<R, P>) {
return class extends Component {
getSomething(): R {
return getSomething(this.props);
}
}
} We really need to work on microsoft/TypeScript-Handbook#574. |
@andy-ms |
This appears to be possible but it's ugly: abstract class AbstractConstructorExample {}
type AbstractConstructable = typeof AbstractConstructorExample;
interface Extended {
getSomething(): number;
}
function extend<Cls extends AbstractConstructable>(cls: Cls): Cls & { new(): Extended } {
return class extends (cls as any) {
getSomething() {
return 0;
}
} as any;
}
abstract class MyAbstractClass { a() { return 0; } }
const SubClass = extend(MyAbstractClass);
new SubClass().getSomething(); |
@andy-ms almost, but if it's an abstract class with generics... :) If I have Thanks for helping me out! |
Looks like #17572 |
@mhegazy yep, seems like. But the question is is typeof for a class with generics ( My use case for it is a mixin which adds mandatory methods to the class which is being extended. I'm building a semantic accessibility API and I have a base class like |
In other issues I saw requests for something like |
the fix as tracked by #17572, is to allow adding |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Hi, I want to have a function (with generics) which takes an abstract class and returns a derived class with the implemented abstract methods.
The example of what I have now:
But it allows to instantiate MyClass and I'd like to have it like this:
Is there a way to make it work?
The text was updated successfully, but these errors were encountered: