Generic type for ngrx Actions in Angular 7

Viewed 555

I have dynamic action:

export class ChangeCurrentConfig implements Action {
  readonly type = ActionTypes.ChangeCurrentConfig;

  constructor(public payload: { key: string, value: any }) {}
}

Where key is dynamic key of the reducer. For example i have:

interface Config1 {
  myPath1: string
}

interface Config2 {
  myPath2: string
}

And i load them dynamically in CurrentConfig reducer. And in effects of every config i handle changes for every config. For example Config1 effect:

@Effect()
  changeMyPath1$ = this.actions$.pipe(
    ofType(ChangeMyPath1),
    map((action: ChangeMyPath1) => action.payload.myPath1),
    map(value => {
      return new ChangeCurrentActivityConfig({
        key: 'myPath1',
        value,
      });
    })
  );

The problem is that there is easy to make a mistake in this line key: 'myPath1',. I want to prevent this with generic for ChangeCurrentConfig action. I tried:

export class ChangeCurrentConfig<T> implements Action {
  readonly type = ActionTypes.ChangeCurrentConfig;

  constructor(public payload: { key: keyof T, value: any }) {}
}

It works good, but i want to make this generic type required for new instances of the class. So if i write in effects new ChangeCurrentActivityConfig without generic type it will show an error. How i can achieve this?

0 Answers
Related