I created an actions file with these 3 actions:
export const showLoading = createAction(`[application] Show warning notification`);
export const hideLoading = createAction(`[application] Show error notification`);
export const showHttpResponseError = createAction(`[application] Show success notification`, props<{ error: HttpErrorResponse, nextAction: Action }>());
export type Actions = ReturnType<
typeof showLoading |
typeof hideLoading |
typeof showHttpResponseError
>;
Then I created an effects file like this:
@Injectable()
export class ApplicationEffects
{
constructor(private actions$: Actions, private dialogsService: DialogsService, public notificationsService: NotificationService, private router: Router) { }
@Effect() public erroHandler$ = this.actions$
.pipe(
ofType(ApplicationActions.showHttpResponseError.type),
switchMap(action => {
const emptyAction = { type: 'noop' };
const error = HttpErrorHandler.handle(action.error);
if (error.redirectTo != null) {
this.router.navigate([error.redirectTo]);
return of(emptyAction);
}
if (action.error.status === 400) {
this.notificationsService.notifyWarning('AVISO', error.messages);
}
else {
this.dialogsService.errorDialog('ERRO', error.messages[0]);
}
return action.nextAction ? of(action.nextAction) : of(emptyAction);
},
));
}
But for some reason, the VS Code intellisense is not recognizing the action type in the switchMap
, it says it's of type never
:
Am I missing something? Or how could I enforce the type of it, since the action is created using ngrx action creators I don't have an explicity type for it.
For completeness, there are multiple ways:
1) type of the ofType
operator
ofType<AddAction>(CounterActions.AddAction)
2) type the injected actions, like you already answered (starting from NgRx 7)
constructor(private actions$: Actions<CounterActions.Actions>
3) use createEffect
in combination with createAction
(starting from NgRx 8)
foo$ = createEffect(() => this.actions$.pipe(
ofType(addAction),
...
);