Angular 2 rxjs switchmap returns Observable <Object>

I am creating a typescript angular 2 application and using rxjs. I follow the example here:

https://angular.io/docs/ts/latest/tutorial/toh-pt6.html#!#stq=formgroup&stp=1

That's it, although I'm trying to hard-print my return signatures using typescript. This may be my problem in that you should not. But it looks like you should be able to.

Suppose I have a service that is dialed to return an Observable>.

 public search(term: string) : Observable<Array<MyModel>> { // http call }

In my component, I am trying to listen to this observable stream and return results.

 private search = new Subject<Search>();
 results: Observable<Array<MyModel>>;

 ngOnInit() {
     this.results = this.search
        .debounceTime(400)
        .distinctUntilChanged()
        .switchMap(search => {
            return service.search(search.term); // returns Observable<Array<MyModel>>
        });
  }

This does not compile with the message Cannot convert type Observable<Object> to type Observable<MyModel[]>>

, switchmap Observable . , ? typescript ?

+4
2

Typescript , :

:

.switchMap((search): Observable<Array<MyModel>> => {
        return service.search(search.term); // returns Observable<Array<MyModel>>
    });

, "" :

 results: Observable<any>;
+1

. , .

ngOnInit() :

this.results = this.search
    .debounceTime(..)
    ...

Observable, result (Observable<Array<MyModel>>), Subject generic Search.

switchmap() Observable. .

:

this.results = Observable<MyModel[]>this.search
    .debounceTime(..)
    ...
0

Source: https://habr.com/ru/post/1650923/


All Articles