Crowdpleasr
Crowdpleasr

Reputation: 4044

Why do I need to use pipe with shareReplay?

I'm confused why the following doesn't compile:


login(email: string, password: string): Observable<any> {
    return this.http.post<authObj>('/users/login',{"email": email, "password": password})
    .shareReplay()
  }

(I get error Property 'shareReplay' does not exist on type 'Observable<authObj>'.ts(2339)). Note for reference: authObj is:

interface authObj {
  "username": string,
  "email": string
} 

but the following compiles fine:

login(email: string, password: string): Observable<any> {
    return this.http.post<authObj>('/users/login',{"email": email, "password": password})
      .pipe(
        tap(res=> this.setSession),      
        shareReplay()
      );
  }

I would have thought that from the standpoint of shareReplay both constructs are similar/identical in terms of the calling object/input, so I would have thought either of the above should work. Why does only the second construct compile? Tks!

Upvotes: 0

Views: 441

Answers (1)

Adrian Brand
Adrian Brand

Reputation: 21638

The biggest change from RxJs 5 to 6 was tree shakability. In RxJs 5 all the operators were part of the observable class. This means that when you import the Observable class into your project you get the lot, every operator no matter if your were using them or not. This tends to bloat the size of your application even if you only need a small amount of RxJs functionality.

With RxJs 6 the operators were moved out into pipeable functions, this meant that you could no longer use the fluent syntax of the methods but came with the huge advantage of your build only needing to include the functionality you require rather than all of RxJs. This reduced build sizes dramatically.

For more information take a look at:

Upvotes: 4

Related Questions