angulartypescriptrxjs

Angular 18 - RxJS : Subscribe is deprecated


Subscribe will be deprecated. The example I have seen on the RxJS website don't really use any observers. If I remove subscribe from the following code, I get "error TS2349: This expression is not callable. [ng] Type 'Observable' has no call signatures."

if (this.postData.token) {
    this.feedService.feedData(this.postData).subscribe(
      (res: any) => {
       this.feedService.changeFeedData(res.feedData);
        },
        (error: any) => {
          this.toastService.presentToast('Error!');
        }
      );
    }

Solution

  • The subscribe() method itself is not deprecated. The RxJS team just deprecated the signatures that take multiple arguments, mostly because of readability concerns.

    Instead, they'd like you to write:

    if (this.postData.token) {
      this.feedService.feedData(this.postData).subscribe({
        next: (res: any) => {
          this.feedService.changeFeedData(res.feedData);
        },
        error: (error: any) => {
          this.toastService.presentToast('Error!');
        }
      });
    }