RxJS:catchError返回联合类型

时间:2019-05-16 09:50:28

标签: angular typescript rxjs

我的意思是,catchError返回一个Observable联合类型: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>caught: Observable<T>

Page.EMPTY是:

export class Page {
    readonly total: number;
    readonly audits: Array<Audit>;

    public static EMPTY:Page = {total: 0, audits: []};
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

解决方案是覆盖catchError签名:

catchError<Page, never>

看看never type