簡體   English   中英

RxJS:catchError返回union類型

[英]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: []};
}

有任何想法嗎?

解決方案是覆蓋catchError簽名:

catchError<Page, never>

看看從不打字

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM