Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'filter' does not exist on type 'BehaviorSubject<string>'. in rxjs 6

Tags:

rxjs

rxjs6

On upgrade of rx js from 5 to 6, throws below error

[ts] Property 'filter' does not exist on type 'BehaviorSubject'.

Current Behavior

import {BehaviorSubject} from 'rxjs';

tokenSubject: BehaviorSubject<string> = new BehaviorSubject<string>(null);
return this.tokenSubject
                .filter(token => token != null)
                .take(1)
                .switchMap(token => {
                    return next.handle(this.addToken(req));
                });

[ts] Property 'filter' does not exist on type 'BehaviorSubject'.

Environment

 "@angular/common": "^6.0.3",
   "rxjs": "^6.0.0",

Expected behavior

No error and working fine

Previous Behavior

import { BehaviorSubject } from "rxjs/BehaviorSubject";
tokenSubject: BehaviorSubject<string> = new BehaviorSubject<string>(null);
return this.tokenSubject
                .filter(token => token != null)
                .take(1)
                .switchMap(token => {
                    return next.handle(this.addToken(req));
                });

[ts] Property 'filter' does not exist on type 'BehaviorSubject'.

Environment

"@angular/common": "^5.0.3",
  "rxjs": "^5.4.3"
like image 393
user630209 Avatar asked Jul 03 '18 09:07

user630209


1 Answers

The RxJS v5.x to v6 Update Guide says:

The previous coding style of chaining operators has been replaced by piping the result of one operator to another.

Use the piping syntax:

return this.tokenSubject.pipe(
    filter(token => token != null),
    take(1),
    switchMap(token => {
        return next.handle(this.addToken(req));
    })
);
like image 187
Joe Daley Avatar answered Sep 19 '22 12:09

Joe Daley