I am trying to create a function that takes a Class and returns function that can return the Class instance. However, it provides type information only for the base class and have an error when I try to create an instance in createFactory function. Playground
class BaseStore {
public id = 1;
}
class OneStore extends BaseStore {
public action() { }
constructor(public a: string, public b: number) {
super()
}
}
function createFactory<T extends BaseStore, C extends new (...args: any) => T>(Create: C) {
return (...args: ConstructorParameters<C>) => new Create(...args);
}
const oneFactory = createFactory(OneStore);
const one = oneFactory('s', 2);
one.id
one.action();