用latestFrom输入

时间:2019-07-05 06:00:47

标签: redux rxjs ngrx

我有以下选择器

export const featureAdapter: EntityAdapter<IRoute> = createEntityAdapter<IRoute>({
  selectId: model => model.routeId,
});

export interface State extends EntityState<IRoute> {
  selectedRouteId: string;
  selectedPointId: string;
}

export const selectAllEntities: (state: object) => Dictionary<IRoute> = featureAdapter.getSelectors(selectRouteState).selectEntities;

export const selectedR: MemoizedSelector<object, string> = createSelector(selectRouteState, getSelectedRoute);

export const selectedRoute: MemoizedSelector<object, IRoute> = createSelector(
      selectAllEntities,
      selectedR,
      (entities, id) => entities[id]
    );

返回类型为IRoute

的内容

我使用的是最新的

  onAction$ = this.actions$.pipe(
    ofType<featureActions.onAction>(featureActions.ActionTypes.onAction),
    concatMap(action =>
      of(action).pipe(
        withLatestFrom(this.store$.pipe(select(RoutesStoreSelectors.getById(), {routeId: action.payload.routeId}))),
      )
    ),
    switchMap(([action, route]) => {})
 )

内部
switchMap(([action, route]) 

route变量的类型为any。但它应该是IRoute

类型

我如何使其正常工作?

2 个答案:

答案 0 :(得分:1)

我不确定为什么需要掌握action中已分发的switchMap,但是您可以执行以下操作以获取类型route的{​​{1}}-

IRoute

具有这样的代码将确保已调度动作的类型为onAction$ = this.actions$.pipe( ofType<featureActions.onAction>(featureActions.ActionTypes.onAction), switchMap(action => combineLatest(of(action), this.store$.pipe(select(RoutesStoreSelectors.getById(), {routeId: action.payload.routeId}))) ), switchMap(([action, route]) => {}) ) ,而action的类型为route

希望对您有帮助。

答案 1 :(得分:1)

我看不出为什么TypeScript无法推断类型,但是您始终可以明确指定类型。

  onAction$ = this.actions$.pipe(
    ofType<featureActions.onAction>(featureActions.ActionTypes.onAction),
    concatMap(action =>
      of(action).pipe(
        withLatestFrom(this.store$.pipe(select(RoutesStoreSelectors.getById(), {routeId: action.payload.routeId}))),
      )
    ),
    switchMap(([action, route]: [Action, IRoute]) => {})
 )