Skip to content

Latest commit

 

History

History
78 lines (57 loc) · 1.96 KB

no-implicit-any-catch.md

File metadata and controls

78 lines (57 loc) · 1.96 KB

Use type-safe error handlers (no-implicit-any-catch)

This rule requires an explicit type annotation for error parameters in error handlers. It's similar to the TypeScript no-implicit-any-catch rule, but is for observables - not try/catch statements.

Rule details

Examples of incorrect code for this rule:

import { throwError } from "rxjs";
import { catchError } from "rxjs/operators";

throwError(() => new Error("Kaboom!")).pipe(
  catchError((error) => console.error(error))
);
import { throwError } from "rxjs";

throwError(() => new Error("Kaboom!")).subscribe({
  error: (error) => console.error(error)
});
import { throwError } from "rxjs";
import { tap } from "rxjs/operators";

throwError(() => new Error("Kaboom!")).pipe(
  tap(undefined, (error) => console.error(error))
);

Examples of correct code for this rule:

import { throwError } from "rxjs";
import { catchError } from "rxjs/operators";

throwError(() => new Error("Kaboom!")).pipe(
  catchError((error: unknown) => console.error(error))
);
import { throwError } from "rxjs";

throwError(() => new Error("Kaboom!")).subscribe({
  error: (error: unknown) => console.error(error)
});
import { throwError } from "rxjs";
import { tap } from "rxjs/operators";

throwError(() => new Error("Kaboom!")).pipe(
  tap(undefined, (error: unknown) => console.error(error))
);

Options

This rule accepts a single option which is an object with an allowExplicitAny property that determines whether or not the error variable can be explicitly typed as any. By default, the use of explicit any is forbidden.

{
  "rxjs/no-implicit-any-catch": [
    "error",
    { "allowExplicitAny": true }
  ]
}

Further reading