It's interesting that catchError
is returning an Observable
union type as Observable<{} | Page}
instead of just Observable<Page>
.
The error message from the compiler reads:
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
The relevant code snippet is shown below:
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 issue lies with the catchError
function because of the following declaration:
export declare function catchError<T, R>(selector: (err: any, caught: Observable<T>) => ObservableInput<R>): OperatorFunction<T, T | R>;
You can see that it returns a type of
: OperatorFunction<T, T | R>
and has a parameter of caught: Observable<T>
.
The definition of Page.EMPTY
is:
export class Page {
readonly total: number;
readonly audits: Array<Audit>;
public static EMPTY:Page = {total: 0, audits: []};
}
Any thoughts on how to resolve this?