rxjs: Typescript - .pipe(catchError()) type issues.

source.pipe(catchError(e=>throwError(new Error("SOMETHING BAD HAPPENED"))))
source.subscribe((r:Author)=>console.log("GOOD"),err=>console.log("ERROR))

basically there is an issue with catchError because it will mess up with the type resulting in this error like this :

error TS2322: Type 'Observable<{}>' is not assignable to type 'Observable<Author>'.
  Type '{}' is not assignable to type 'Author'.
    Property 'title' is missing in type '{}'.

I created a work around this but maybe there is a better way.

  private catchHttpError = <T>() =>
    catchError<T, T>((error: HttpErrorResponse) => {
      const msg = `${error.status} ${error.statusText} -  ${error.url}`;

      return throwError(new Error(msg));
    });

  private apiPipe = <T>() => pipe(share(), this.catchHttpError<T>());

When i pass the type to my apiPipe function and to catchError operator no errors comeout but it looks dirty to me.

Anyone have ideas?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

@benlesh thank you for the example, this works.

Given that I haven’t seen a minimal reproduction of this issue where some other typing wasn’t incorrect, I’m going to close this issue.

Here’s a minimal version of what people think is going on that does, in fact, compile:

import { Observable, of, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

const result: Observable<number> = of(1, 2, 3).pipe(
  catchError(err => throwError(new Error('lol'))),
);

If you hit this issue, you probably needed to properly type the source going into your catchError.