I'm currently working on converting a group of class constructors into a mapped object with properties that are functions returning instances.
For instance:
// Auto-generated utility from external source:
class FooClient { getData() {}}
class BarClient { getData() {}}
export const Clients = {
FooClient,
BarClient
}
This can then be used like:
import { Clients } from './somemodule';
type ClientTypes = typeof Clients;
function getFooData(c: ClientTypes) {
const fooClient = new c.FooClient();
return fooClient.getData();
}
What I want to achieve is replacing the classes / constructors with factory functions:
type Factories = {
[Property in keyof ClientTypes as `use${Property}`]: () => ClientTypes[Property]
}
function getFooData(f: Factories) {
const fooClient = new (f.useFooClient()); // <-- use function returns the original constructor type
return fooClient.getData();
}
My query is, how can I adjust the return type in the mapped type so that it doesn't return a constructor but rather the value as if the constructor had been invoked.
For example:
function getFooData(f: Factories) {
const fooClient = f.useFooClient(); // <-- instead returns an instance
return fooClient.getData();
}
Is there any specific keyword I should use to indicate that I want my function to return an instance of a type rather than the type itself?