Skip to content

Commit 329d45b

Browse files
docs(no-misused-observables): examples & description
1 parent 01a327a commit 329d45b

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

docs/rules/no-misused-observables.md

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,63 @@
66

77
<!-- end auto-generated rule header -->
88

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+
41+
Examples of **correct** code for this rule:
42+
43+
```ts
44+
import { of } from "rxjs";
45+
46+
[1, 2, 3].map(i => of(i));
47+
48+
interface MyRxInterface {
49+
foo(): Observable<number>;
50+
}
51+
class MyRxClass implements MyRxInterface {
52+
foo(): Observable<number> {
53+
return of(42);
54+
}
55+
}
56+
```
57+
958
## Options
1059

1160
<!-- WARNING: not auto-generated! -->
1261

13-
| Name | Description | Type | Default |
14-
| :----------------- | :--------------------------------------------------------------------------------------------------------------------------------------- | :------ | :------ |
15-
| `checksSpreads` | Disallow `...` spreading an Observable. | Boolean | `true` |
16-
| `checksVoidReturn` | Disallow returning an Observable from a function typed as returning `void`. | Object | `true` |
62+
| Name | Description | Type | Default |
63+
| :----------------- | :-------------------------------------------------------------------------- | :------ | :------ |
64+
| `checksSpreads` | Disallow `...` spreading an Observable. | Boolean | `true` |
65+
| `checksVoidReturn` | Disallow returning an Observable from a function typed as returning `void`. | Object | `true` |
1766

1867
### `checksVoidReturn`
1968

@@ -27,3 +76,7 @@ You can disable selective parts of the `checksVoidReturn` option. The following
2776
| `properties` | Disallow providing an Observable-returning function where a function that returns `void` is expected by a property. | Boolean | `true` |
2877
| `returns` | Disallow returning an Observable-returning function where a function that returns `void` is expected. | Boolean | `true` |
2978
| `variables` | Disallow assigning or declaring an Observable-returning function where a function that returns `void` is expected. | Boolean | `true` |
79+
80+
## Further reading
81+
82+
- [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

Comments
 (0)