I am currently developing a TypeScript Angular 2 application and utilizing RxJS. I am following the example given in this tutorial:
https://angular.io/docs/ts/latest/tutorial/toh-pt6.html#!#stq=formgroup&stp=1
Although I am attempting to strongly type my return signatures with TypeScript, it seems that this may be causing an issue as it is not recommended. However, it feels like there should be a way to do so.
Let's say I have a service that is expected to return an Observable of an array of MyModel objects.
public search(term: string): Observable<Array<MyModel>> { // http call }
In my component, I am trying to subscribe to this observable stream and retrieve the 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>>
});
}
However, I encounter a compilation error stating
Cannot convert type Observable<Object> to type Observable<MyModel[]>>
I am unsure why switchmap is returning an Observable instead of the expected type. Do I need to map it again before assigning it to 'results'? Is this related to TypeScript typings and the return signature of switchmap?