Issue
How to extend a generic type with a constructor function in Typescript?
function doSomething<T extends ?constructor?>(constructor: T) {
...
}
Solution
You could do like this:
type Constructor = new (...args: any[]) => any;
class Base {
baseProp = 'base value';
}
function doSomething<T extends Constructor>(constructor: T) {
return class extends constructor {
factoryProp = 'factory prop';
}
}
const DerivedClass = doSomething(Base);
const instance = new DerivedClass;
console.log(instance.baseProp);
console.log(instance.factoryProp);
Answered By - Guerric P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.