简体   繁体   English

RxJS:catchError返回union类型

[英]RxJS: catchError returns union type

I mean, catchError returns an Observable union type: Observable<{} | Page} 我的意思是, catchError返回一个Observable联合类型: Observable<{} | Page} Observable<{} | Page} instead of Observable<Page> . Observable<{} | Page}而不是Observable<Page>

I'm getting this compiler message: 我收到这个编译器消息:

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

Related code is: 相关代码是:

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()
        );
}

The problem is located on catchError since: 问题出在catchError因为:

export declare function catchError<T, R>(selector: (err: any, caught: Observable<T>) => ObservableInput<R>): OperatorFunction<T, T | R>;

As you can see it's returning an : OperatorFunction<T, T | R> 你可以看到它返回一个: OperatorFunction<T, T | R> : OperatorFunction<T, T | R> and also caught: Observable<T> . : OperatorFunction<T, T | R>并且还caught: Observable<T>

Page.EMPTY is: Page.EMPTY是:

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

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

Any ideas? 有任何想法吗?

The solution is about overriding catchError signature: 解决方案是覆盖catchError签名:

catchError<Page, never>

Take a look on never type . 看看从不打字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM