使用NGRX 8和createAction,如何使用强类型@Effect?

时间:2019-12-18 13:01:28

标签: ngrx ngrx-effects

我正在将大型代码库从NGRX 7迁移到8。

我的动作如下:

export class DeleteRecordAction implements Action {
  readonly type = ActionTypes.DELETE_RECORD;
  constructor(public payload: IRecord);
}
export class DeleteRecordSuccessAction implements Action {
  readonly type = ActionTypes.DELETE_RECORD_SUCCESS;
}
export class DeleteRecordFailureAction implements Action {
  readonly type = ActionTypes.DELETE_RECORD_FAILURE;
}

和我的效果看起来像这样(注意强类型的Return Observable):

@Effect() deleteRecord$: Observable<DeleteRecordSuccessAction | DeleteRecordFailureAction> =
  this.actions$.pipe(
    ofType<Actions.DeleteRecordAction>(ActionTypes.DELETE_RECORD),
    switchMap((action) => this.recordService.deleteRecord(action.payload).pipe(
      map((result) => new Actions.DeleteRecordSuccessAction()),
      catchError((err) => of(new Actions.DeleteRecordFailureAction())),
    )),
  );

我正在尝试将操作迁移为使用新的createAction语法,这很容易:

export const DeleteRecordAction = createAction('[FOO] Delete Record', props<{ payload: IRecord }>());
export const DeleteRecordSuccessAction = createAction('[FOO] Delete Record Success');
export const DeleteRecordFailureAction = createAction('[FOO] Delete Record Failure');

但是现在我的@Effect不会在以下行编译:

@Effect() deleteRecord$: Observable<DeleteRecordSuccessAction | DeleteRecordFailureAction> =

出现错误:

  

TS2749:“ DeleteRecordSuccessAction”引用一个值,但在此用作类型。
  TS2749:“ DeleteRecordFailureAction”是指一个值,但此处被用作类型。

我可以从@Effect返回值中删除强类型,而只需使用通用Action,如下所示:

@Effect() deleteRecord$: Observable<Action> =

,但是随后我丢失了返回值的有用的类型检查,从而使代码更容易出错。 新的createEffect方法似乎无济于事。有没有一种方法可以使用NGRX 8并保持特技效果?还是我应该坚持使用NGRX 7语法?

0 个答案:

没有答案