Disallow passing separate handlers to subscribe and tap (rxjs-x/prefer-observer)

April 18, 2025 ยท View on GitHub

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, ๐Ÿ”’ strict.

๐Ÿ”ง๐Ÿ’ก This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

๐Ÿ’ญ This rule requires type information.

This rule effects failures if subscribe - or tap - is called with separate handlers instead of an observer.

Rule details

Examples of incorrect code for this rule:

import { of } from "rxjs";
of(42, 54).subscribe((value) => console.log(value));

Examples of correct code for this rule:

import { of } from "rxjs";
of(42, 54).subscribe({
  next: (value) => console.log(value)
});

Options

NameDescriptionTypeDefault
allowNextAllows a single next callback.Booleantrue

This rule accepts a single option which is an object with an allowNext property that determines whether a single next callback is allowed. By default, allowNext is true.

{
  "rxjs-x/prefer-observer": [
    "error",
    { "allowNext": false }
  ]
}

When Not To Use It

If you rely on RxJS's deprecation of separate handlers and don't need to double-flag usage, then you don't need this rule.

Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

Further reading

Resources