Search code examples
angularrxjsngrxngrx-effects

withLatestFrom in effect wrong type


I have the following selector :

export const selectMissionState: MemoizedSelector<object, State> = createFeatureSelector<State>('missions');

export const selectAll: (state: object) => Array<IMission> = featureAdapter.getSelectors(selectMissionState).selectAll;

In my effect I do the following =>

  @Effect()
  applyMissionReroute$ = this.actions$.pipe(
    ofType<featureActions.ApplyMissionReroute>(featureActions.ActionTypes.ApplyMissionReroute),
    withLatestFrom(MissionsStoreSelectors.selectAll()),
    switchMap(([action, state]) => {})
  );

But I face a problem, Although my selectAll is supposed to return an Array (which is what the compiler say when I over, my [action, state] is just a IMission.

What My state is a just a var IMission and not a Array ?


Solution

  • You should call withLatestFrom either with this.store to receive entire store state, or with this.store.pipe(select(...selector...)) for only a slice of state.

    With this change below, you'll receive result of selector inside selectAll argument.

    @Effect()
    applyMissionReroute$ = this.actions$.pipe(
      ofType<featureActions.ApplyMissionReroute
        (featureActions.ActionTypes.ApplyMissionReroute),
      withLatestFrom(this.store.pipe(select(MissionsStoreSelectors.selectAll))),
      switchMap(([action, selectAll]) => {
        ..
      })
    );
    

    Hope it will help you.