Search code examples
typescriptrxjs6angular8switchmap

RXJS v6.4 switchMap operator returns an Observable rather than the result (accordinng to TypeScript linter)


I am running an Angular 8 app, and I am getting an error which seems to be a major bug in RxJS or I am missing something.

import { of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
of(1,2,3)
  .pipe(
    switchMap((x) => of(x + 1))
  )

Now I would expect a subscriber to receive the following output:

//2, 3, 4

But instead, its outputting the actual observable that I return in the switchMap operation:

//Observable<number>, Observable<number>, Observable<number>

SUMMARY

Essentially it seems that switchMap is returning a value of type Observable<Observable<number>> instead of just Observable<number>.

I am not sure if this if this is a TypeScript or RxJS error, but it seems that when I revert RxJS to version 6.0.0 the issue goes away.


Versions:

  • Angular 8.0.0
  • TypeScript 3.4.5
  • RxJS 6.4.0

I've just tested and dthe same thing is happening with flatMap


Solution

  • Not sure what the issue was here but I managed to resolve it. One of the following steps must have done it, but I don't know which one: