|
| 1 | +# Disallow Observables in places not designed to handle them (`rxjs-x/no-misused-observables`) |
| 2 | + |
| 3 | +💼 This rule is enabled in the 🔒 `strict` config. |
| 4 | + |
| 5 | +💭 This rule requires [type information](https://typescript-eslint.io/linting/typed-linting). |
| 6 | + |
| 7 | +<!-- end auto-generated rule header --> |
| 8 | + |
| 9 | +This rule forbids providing observables to logical locations where the TypeScript compiler allows them but they are not handled properly. |
| 10 | +These situations can often arise due to a misunderstanding of the way observables are handled. |
| 11 | + |
| 12 | +> [!TIP] |
| 13 | +> `no-misused-observables` only detects code that provides observables to incorrect _logical_ locations. |
| 14 | +> See [`no-floating-observables`](./no-floating-observables.md) for detecting unhandled observable _statements_. |
| 15 | +
|
| 16 | +This rule is like [no-misused-promises](https://typescript-eslint.io/rules/no-misused-promises) but for Observables. |
| 17 | + |
| 18 | +> [!NOTE] |
| 19 | +> Unlike `@typescript-eslint/no-misused-promises`, this rule does not check conditionals like `if` statements. |
| 20 | +> Use `@typescript-eslint/no-unnecessary-condition` for linting those situations. |
| 21 | +
|
| 22 | +## Rule details |
| 23 | + |
| 24 | +Examples of **incorrect** code for this rule: |
| 25 | + |
| 26 | +```ts |
| 27 | +import { of } from "rxjs"; |
| 28 | + |
| 29 | +[1, 2, 3].forEach(i => of(i)); |
| 30 | + |
| 31 | +interface MySyncInterface { |
| 32 | + foo(): void; |
| 33 | +} |
| 34 | +class MyRxClass implements MySyncInterface { |
| 35 | + foo(): Observable<number> { |
| 36 | + return of(42); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +const a = of(42); |
| 41 | +const b = { ...b }; |
| 42 | +``` |
| 43 | + |
| 44 | +Examples of **correct** code for this rule: |
| 45 | + |
| 46 | +```ts |
| 47 | +import { of } from "rxjs"; |
| 48 | + |
| 49 | +[1, 2, 3].map(i => of(i)); |
| 50 | + |
| 51 | +interface MyRxInterface { |
| 52 | + foo(): Observable<number>; |
| 53 | +} |
| 54 | +class MyRxClass implements MyRxInterface { |
| 55 | + foo(): Observable<number> { |
| 56 | + return of(42); |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## Options |
| 62 | + |
| 63 | +<!-- WARNING: not auto-generated! --> |
| 64 | + |
| 65 | +| Name | Description | Type | Default | |
| 66 | +| :----------------- | :-------------------------------------------------------------------------- | :------ | :------ | |
| 67 | +| `checksSpreads` | Disallow `...` spreading an Observable. | Boolean | `true` | |
| 68 | +| `checksVoidReturn` | Disallow returning an Observable from a function typed as returning `void`. | Object | `true` | |
| 69 | + |
| 70 | +### `checksVoidReturn` |
| 71 | + |
| 72 | +You can disable selective parts of the `checksVoidReturn` option. The following sub-options are supported: |
| 73 | + |
| 74 | +| Name | Description | Type | Default | |
| 75 | +| :----------------- | :--------------------------------------------------------------------------------------------------------------------------------------- | :------ | :------ | |
| 76 | +| `arguments` | Disallow passing an Observable-returning function as an argument where the parameter type expects a function that returns `void`. | Boolean | `true` | |
| 77 | +| `attributes` | Disallow passing an Observable-returning function as a JSX attribute expected to be a function that returns `void`. | Boolean | `true` | |
| 78 | +| `inheritedMethods` | Disallow providing an Observable-returning function where a function that returns `void` is expected by an extended or implemented type. | Boolean | `true` | |
| 79 | +| `properties` | Disallow providing an Observable-returning function where a function that returns `void` is expected by a property. | Boolean | `true` | |
| 80 | +| `returns` | Disallow returning an Observable-returning function where a function that returns `void` is expected. | Boolean | `true` | |
| 81 | +| `variables` | Disallow assigning or declaring an Observable-returning function where a function that returns `void` is expected. | Boolean | `true` | |
| 82 | + |
| 83 | +## Further reading |
| 84 | + |
| 85 | +- [TypeScript void function assignability](https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-returning-non-void-assignable-to-function-returning-void) |
0 commit comments