Lev
Lev

Reputation: 15714

how to define the type of an observer in typescript

Typescript is asking me to type my observer variable :

Observer.create(observer: whatTypeShouldIUse => /* do things with observer */)

I tried using the Observer class or interface from the rxjs library, but it's a generic type.

What type should I put for my observer ? I put Observer<any> for now...

Upvotes: 6

Views: 6150

Answers (1)

ThePoet
ThePoet

Reputation: 326

In case you didn't find a solution yet. First, you must import both Observer and Observable libraries.

import { Observable, Observer } from 'rxjs';

Following your question, we do something like this:

Observable.create( (observer: Observer<JSON>) => {
    /* do things with observer */
    observer.next(data); //data - Must be a JSON object
    observer.complete();
})

Note that the observer type argument is the type of the observer.next(data).

Upvotes: 8

Related Questions