我的意思是,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: []};
}
有什么想法吗?