I have a dynamic action that changes the current configuration:
export class UpdateConfig implements Action {
readonly type = ActionTypes.UpdateConfig;
constructor(public payload: { key: string, value: any }) {}
}
The key
parameter is the dynamic key of the reducer. For example, consider two different configurations:
interface Config1 {
myPath1: string
}
interface Config2 {
myPath2: string
}
These configurations are loaded dynamically in the CurrentConfig
reducer. Each configuration has its own effects to handle changes. For instance, the effect for Config1
would look like this:
@Effect()
updateMyPath1$ = this.actions$.pipe(
ofType(UpdateMyPath1),
map((action: UpdateMyPath1) => action.payload.myPath1),
map(value => {
return new UpdateConfig({
key: 'myPath1',
value,
});
})
);
The issue arises when trying to prevent mistakes like specifying the wrong key in the payload. To address this, I introduced a generic type for the UpdateConfig
action. Here's what I tried:
export class UpdateConfig<T> implements Action {
readonly type = ActionTypes.UpdateConfig;
constructor(public payload: { key: keyof T, value: any }) {}
}
While this solution works well, I want to enforce the use of this generic type for creating new instances of the class. In other words, I want it to be required. If someone tries to create a new instance without specifying the generic type, an error should be displayed. How can I achieve this requirement?