How do I use catchError() and still return a typed Observable with rxJs 6.0?

后端 未结 1 604
逝去的感伤
逝去的感伤 2021-02-05 09:23

cdSo I\'m trying to migrate some of my Angular 5 code to 6, and I understand most of the changes required for rxjs to work using the .pipe() operator. It works as you would expe

相关标签:
1条回答
  • 2021-02-05 09:54

    You should be able to explicitly specify the type parameters to catchError to indicate that it won't return an observable that emits a value - that is, it will return Observable<never> - and will always throw:

    getAlbums(): Observable<Album[]> {
        return this.httpClient.get<Album[]>(this.config.urls.url("albums"))
            .pipe(
                map(albumList => this.albumList = albumList),
                catchError<Album[], never>(new ErrorInfo().parseObservableResponseError)
            );           
    }
    

    Doing so will see the type inferred from the pipe call to be Observable<Album[]>.

    0 讨论(0)
提交回复
热议问题