My application features multiple factory functions, each returning an object with specific methods (more details provided below). I am interested in creating a type that combines the properties of all these returned objects.
const factoryA = () => ({
fun1(): string {
return 'fun1';
}
});
const factoryB = () => ({
fun2(): number {
return 2;
}
});
const factoryList = [factoryA, factoryB];
The type I wish to generate automatically from the factoryList
would essentially look like this:
type FactoryListType = ReturnType<typeof factoryA> & ReturnType<typeof factoryB>
Is there a universal approach to derive this type from the factoryList
without explicitly defining it?
Context
This scenario pertains to a graphql server where the factories are responsible for creating DataLoaders specific to the ongoing request. By utilizing factories, the loader cache can be cleared between different requests.
For example:
export const userLoaderFactory = () => ({
user: new DataLoader<string, UserModel>((keys: string[]) => {
return UserModel.query().whereIn('id', keys);
})
});
Subsequently, when the server generates the context for the request, it goes through the list of factories, generates the data loaders, and includes them in the context:
const createContext = () => {
const loaders = loaderFactories.reduce((acc, factory) => {
const factoryLoaders = factory();
return {
...acc,
...factoryLoaders,
};
}, {});
return {
loaders
};
};
My objective is to introduce a Context type in my GraphQL types and resolvers that encompasses the loader type definitions.