Angular 11: subscribe is deprecated: Use an observer instead?
Asked Answered
S

3

19

My tslint gone crazy? It gives warning for every subscribtion I made in whole app. It doesn't matter if I use old or new syntax it still says that subscribe is deprecated... How to write a subscription that will not be deprecated?

That's what was ok till today:

something.subscribe((user: User) => {
        this.userProviderService.setUserId(user.userId);
        this.proceed = true;
      });

I tried new syntax but makes no change:

something.subscribe({
        next: (user: User) =>  {
          this.userProviderService.setUserId(user.userId);
          this.proceed = true;
        },
        complete: () => {},
        error: () => {}
      });

This is exactly what it's saying:

(method) Observable.subscribe(next?: (value: Object) => void, error?: (error: any) => void, complete?: () => void): Subscription (+4 overloads) @deprecated — Use an observer instead of a complete callback

@deprecated — Use an observer instead of an error callback

@deprecated — Use an observer instead of a complete callback

subscribe is deprecated: Use an observer instead of a complete callback (deprecation)tslint(1)

So how do I subscribe to things now?

Sensor answered 12/3, 2021 at 17:51 Comment(3)
Try putting a dummy console in error and complete callbacks it seems to be an issue with empty callbacks for those keys. Many people have faced this issueConvalescent
Does this answer your question? Subscribe is deprecated: Use an observer instead of an error callbackRoscoeroscommon
@Roscoeroscommon what you just linked is related to another problemRhachis
S
18

I just looked up TSLint (v1.3.3) at VS Code extenstions tab. It says:

❗IMPORTANT: TSLint has been deprecated in favor of ESLint.

As I disabled TSLint and installed ESLint, all warnings related to subscribtions dissapeared.

Cheers!

Sensor answered 3/4, 2021 at 17:27 Comment(2)
Props to this answer! When it comes to the error in VS Code, this solves everything. Remove the TSLint extension and install the ESLint extension.Crosseye
If you do this through npm uninstall/install, be sure to restart VSCode for the change to take effect.Tilney
R
12

To answer your question "So how do I subscribe to things now": https://rxjs-dev.firebaseapp.com/guide/observer This is it. It is easy to use and pretty similar to what has been done before with the small change that it actually now accepts an object (observer) with 3 keys: next, error, complete.

We had the same discussion like 2 days ago at work and altough you can/should use the observer the deprecation seems to be a false alarm. (We thought we had to change ~900 subscribes):

This is an issue created on the rxjs github page regarding this issue: https://github.com/ReactiveX/rxjs/issues/6060

And in it the devs say it is due to a typescript bug: https://github.com/microsoft/TypeScript/issues/43053

This bug already has been fixed 3 days ago, i am not sure though if it is already in the newest release:

https://github.com/microsoft/TypeScript/pull/43165

Rhachis answered 12/3, 2021 at 18:9 Comment(2)
Pheew :D So as I understand from this: it's kind of vscode/tslint issue which will probably get fixed soon?Sensor
Thats exactly the caseRhachis
A
7

Latest versions support the following structure:

myObservable.subscribe({
    next: (val) => { ... },
    error: (err) => { ... },
    complete: () => { ... }     
})
Atomy answered 29/1, 2022 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.