I have an object map as follows:
const obj: {
AddRowOperation: typeof RowOperations.AddRowOperation;
DeleteRowOperation: typeof RowOperations.DeleteRowOperation;
FilterRowsOperation: typeof RowOperations.FilterRowsOperation;
... 25 more ...;
ResetRowStatusOperation: typeof RowOperations.ResetRowStatusOperation;
}
I want to map this object to instances of each class. So the type should be:
const obj: {
AddRowOperation: RowOperations.AddRowOperation;
DeleteRowOperation: RowOperations.DeleteRowOperation;
FilterRowsOperation: RowOperations.FilterRowsOperation;
... 25 more ...;
ResetRowStatusOperation: RowOperations.ResetRowStatusOperation;
}
I have tried a lot of options, but the type always ends up being unioned like:
const obj: {
AddRowOperation: (RowOperations.AddRowOperation | RowOperations.DeleteRowOperation | ...);
// etc...
}
I would have thought this was a simple scenario. It would be great to dynamically instantiate the classes rather than manually typing each one out.
Can this be done while keeping typescript happy? The classes do not share a common interface.
Edit
Solution proposed by jaclz:
type InstanceTypeProps<T extends Record<keyof T, new (...args: any) => any>> = { [K in keyof T]: InstanceType<T[K]> };
class A {}
class B {}
class C {}
const obj = { A, B, C }
type mapOfInstances = InstanceTypeProps<typeof obj>
InstanceTypePropsfrom the link, but without a testable example it's hard to tell. If that works for you I could potentially come up with my own example code for the answer, but ideally it would be in the question itself.