I try to declare (interface only) factory which provides any generic types from own class to result.
At first, I declare any classes. Foo is base class. Moo is some user class.
declare module 'any-module' {
class Foo<T> {
get test(): T;
}
class Moo<T> extends Foo<T> {
get moo(): T;
}
}
Then, I try to declare factory which know about Foo but do not know about Moo. This factory must be produce Foo-like classes by instance from extends of Foo (Moo for example).
interface IFactory<T> {
create(Instance) => ??? /* like: new Instance<T>() */
}
So, I tryed any ways for that:
interface IFactory<T> {
create1<R extends Foo<T>>(instance: new (...ars: any) => R): R;
create2<X extends new (...ars: any) => Foo<T>>(instance: X): InstanceType<X>;
}
but no of above ways does not work correctly
// Getting factory with string generic
const factory = {} as IFactory<string>;
factory.create1(Moo) // Generic type is string, but result is Foo
factory.create2(Moo) // Result is Moo, but generic type is unknown
Thanks!