[英]RxJS: catchError returns union type
我的意思是, catchError
返回一個Observable
聯合類型: Observable<{} | Page}
Observable<{} | Page}
而不是Observable<Page>
。
我收到這個編譯器消息:
Type 'Observable<{} | Page>' is not assignable to type 'Observable<Page>'.
Type '{} | Page' is not assignable to type 'Page'.
Type '{}' is missing the following properties from type 'Page': total, audits
相關代碼是:
public searchFromStorageByCriteria(filter: Query): Observable<Page> {
const buildPage = () => map((response: Response) => this.buildPage(response));
const onErrorEmptyPage = () => catchError(() => Observable.of(Page.EMPTY));
let url:string = this.buildURL(user, app, filter);
return this.authHttp.get(url)
.pipe(
buildPage(),
onErrorEmptyPage()
);
}
問題出在catchError
因為:
export declare function catchError<T, R>(selector: (err: any, caught: Observable<T>) => ObservableInput<R>): OperatorFunction<T, T | R>;
你可以看到它返回一個: OperatorFunction<T, T | R>
: OperatorFunction<T, T | R>
並且還caught: Observable<T>
。
Page.EMPTY
是:
export class Page {
readonly total: number;
readonly audits: Array<Audit>;
public static EMPTY:Page = {total: 0, audits: []};
}
有任何想法嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.