I have two sets of TypeScript definitions that are similar:
enum OperationType1 {
start = 'start',
stop = 'stop'
}
type ConfigForOperationType1 = { [key in OperationType1]: TestConfig };
enum OperationType2 {
play = 'play',
pause = 'pause'
}
type ConfigForOperationType2 = { [key in OperationType2]: TestConfig };
I can use them as shown below:
const config1: ConfigForOperationType1 = {
[OperationType1.start]: { ... },
[OperationType1.stop]: { ... }
};
const config2: ConfigForOperationType2 = {
[OperationType2.play]: { ... },
[OperationType2.pause]: { ... }
};
Now, I want to replace ConfigForOperationType1
and ConfigForOperationType2
with a single generic type called OperationConfig
, taking Operation
as a parameter like this:
type GenericOperationConfig<T> = { [key in T]: TestConfig };
const config1: GenericOperationConfig<OperationType1> = { ... };
const config2: GenericOperationConfig<OperationType2> = { ... };
The code above is causing an error and throws the following exception:
TS2322: Type 'T' is not assignable to type 'string | number |
symbol'. Type 'T' is not assignable to type 'symbol'.
The question. How can I make the mapped object type (GenericOperationConfig) accept parameters? Or is there another convenient alternative?