class Foo {
name: string;
constructor({name}: {name: string}) {
this.name = name;
}
}
class Bar<T extends Foo> {
foo: T;
constructor({foo}: {foo: T}) {
this.foo = foo;
}
}
class CustomFoo extends Foo {
xxx: string;
constructor({
name,
xxx,
}: {
name: string,
xxx: string,
}) {
super({name});
this.xxx = xxx;
}
}
class CustomBar<F extends Foo> extends Bar<F> {
customField: string;
constructor({
foo,
customField,
}: {
foo: F,
customField: string,
}) {
super({foo});
this.customField = customField;
}
}
const doSomething = <
B extends Bar<F>,
F extends Foo,
>(
FooConstructor: { new(...args : any[]): F; },
BarConstructor: { new(...args : any[]): B; },
): B => {
return new BarConstructor({});
}
const mything = doSomething(CustomFoo, CustomBar);
Unfortunately the type of mything
is Bar<CustomFoo>
instead of CustomBar<CustomFoo>
.
It seems TypeScript fails to infer the correct return type.
How can I modify doSomething
to return the expected inferred type?
I am unsure about potential solutions to this issue.