Context 'this' of Eventemitter<string> is not attributable to the 'this' method of type Observable<string>

Asked

Viewed 208 times

0

I looked at the example in a Github repository. The application runs successfully and the written code works normally, but I have the following error in my Visual Studio Code:

The 'this' context of type 'Eventemitter' is not Assignable to method’s 'this' of type 'Observable'. n Types of Property 'lift' are incompatible. Type '(Operator: Operator) => Observable' is not Assignable to type '(Operator: Operator) => Observable'. Type 'Observable' is not Assignable to type 'Observable'. n Type 'string' is not Assignable to type 'R'.

The code that is generating the error is as follows:

this.notificationService.notifier
    .do(message => {
      this.message = message;
      this.snackVisibility = 'visible'; 
    })
    .switchMap(message => Observable.timer(2000))
    .subscribe(timer => this.snackVisibility = 'hidden');

Although the application works perfectly VS Code presents me the error as shown in the image:

inserir a descrição da imagem aqui

What would be the best solution for this error?

  • 1

    If you can put in your code this.notificationService.notifier, kindly

2 answers

0


After doing some research, I understood that Eventemitter is used of a Child par Aum Parent. To make service communications for the component you need to use Subject.

So the component code went like this:

this.notificationService.notifier$
                        .subscribe(message => {
                          this.message = message;
                          this.snackVisibility ='visible';
                          setTimeout(() => {
                            this.snackVisibility = 'hidden';
                          }, 2000);
                        });

And the service code went like this:

import { Subject } from "rxjs/Subject";

export class NotificationService {
    private notifier = new Subject<string>();
    public notifier$ = this.notifier.asObservable();

    notify(message: string) {
        this.notifier.next(message);
    }
}

0

In your service you must have something like this:

notifier = new EventEmitter<string>();

Another solution may be you change from string for any, because "in that scenario", it makes sense.

Would that way:

notifier = new EventEmitter<any>();

A hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.