In typescript, given an object represented as a record of derived class, how do I extract the proper underlying class ?
Let me give you an exemple of my problem:
abstract class factory {
abstract deploy(...[]): any;
}
class a_factory extends factory {
deploy = (a: number, b: string) => {};
}
class b_factory extends factory {
deploy = (a: {}, b: number) => {};
}
const obj = {
a: a_factory,
b: b_factory
};
export type Object<F extends factory> = ReturnType<F['deploy']>;
export interface ContractBuilder<F extends factory> {
deploy(...args: Parameters<F['deploy']>): Object<F>;
}
export type FactoryConstructor<F extends factory> = {
new (): F;
};
const doSome = (any: any) => {};
const build = <K extends factory, F extends Record<string, FactoryConstructor<K>>>(contracts: F) => {
const a: { [contractName: string]: any } = {};
for (const x in contracts) {
a[x] = doSome(contracts[x] as any);
}
type MySuperType<T extends F> = {
[P in keyof T]: ContractBuilder<K>;
};
return a as MySuperType<F>;
};
const MyTestObj = build(obj);
MyTestObj.a.deploy() // This is not typed to a_factory
MyTestObj.b.deploy() // This is not typed to b_factory
As you can see, the final object doesn't infer the correct type for each of his member.
How could I achieve that ?