Search code examples
angularrxjsrxjs5rxjs6rxjs-compat

Property 'map' does not exist on type 'Observable' after upgrading rxjs to 6


I upgraded my Angular application from version 5.2 to 6.0 with the instructions from https://update.angular.io.

Now my Angular application doesn't build because of the "rxjs-5-to-6-migrate" migration:

ERROR in bla.ts: error TS2339: Property 'map' does not exist on type 'Observable'.

I have the following imports:

import { Observable } from 'rxjs/observable';
import { of } from 'rxjs/observable/of';
import { map } from 'rxjs/operators';

If I change the imports like this it works:

import { Observable } from 'rxjs/observable';
import 'rxjs/Rx';

But I don't understand why... I want to use the explicit imports and not import all operators.


UPDATE: As some answers pointed out I have to use pipes to be able to use operators. This was my problem because I thought I can still chain the operators to the observables.

Old Style:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

this.http.get('/api/appsettings/get').map(data => { return true; }).catch(() => { return Observable.of(false); });

New Style

import { of,  Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';

this.http.get('/api/appsettings/get').pipe(map(data => { return true; }), catchError(() => { return of(false); }));

Solution

  • You need to use pipe method on Observable and pass map function inside, like:

    import { of } from 'rxjs';
    import { map } from 'rxjs/operators';
    
    of([1,2,3]).pipe(
      map(i => i*2)
    );