1 | # `no-meaningless-void-operator`
|
2 |
|
3 | Disallow the `void` operator except when used to discard a value.
|
4 |
|
5 | Disallow the `void` operator when its argument is already of type `void` or `undefined`.
|
6 |
|
7 | ## Rule Details
|
8 |
|
9 | The `void` operator is a useful tool to convey the programmer's intent to discard a value. For example, it is recommended as one way of suppressing [`@typescript-eslint/no-floating-promises`](./no-floating-promises.md) instead of adding `.catch()` to a promise.
|
10 |
|
11 | This rule helps an author catch API changes where previously a value was being discarded at a call site, but the callee changed so it no longer returns a value. When combined with [no-unused-expressions](https://eslint.org/docs/rules/no-unused-expressions), it also helps _readers_ of the code by ensuring consistency: a statement that looks like `void foo();` is **always** discarding a return value, and a statement that looks like `foo();` is **never** discarding a return value.
|
12 |
|
13 | Examples of code for this rule:
|
14 |
|
15 |
|
16 |
|
17 | ### ❌ Incorrect
|
18 |
|
19 | ```ts
|
20 | void (() => {})();
|
21 |
|
22 | function foo() {}
|
23 | void foo();
|
24 | ```
|
25 |
|
26 | ### ✅ Correct
|
27 |
|
28 | ```ts
|
29 | (() => {})();
|
30 |
|
31 | function foo() {}
|
32 | foo(); // nothing to discard
|
33 |
|
34 | function bar(x: number) {
|
35 | void x; // discarding a number
|
36 | return 2;
|
37 | }
|
38 | void bar(); // discarding a number
|
39 | ```
|
40 |
|
41 | ## Options
|
42 |
|
43 | This rule accepts a single object option with the following default configuration:
|
44 |
|
45 | ```json
|
46 | {
|
47 | "@typescript-eslint/no-meaningless-void-operator": [
|
48 | "error",
|
49 | {
|
50 | "checkNever": false
|
51 | }
|
52 | ]
|
53 | }
|
54 | ```
|
55 |
|
56 | - `checkNever: true` will suggest removing `void` when the argument has type `never`.
|
57 |
|
58 | ## Attributes
|
59 |
|
60 | - Configs:
|
61 | - [ ] ✅ Recommended
|
62 | - [x] 🔒 Strict
|
63 | - [x] 🔧 Fixable
|
64 | - [x] 💭 Requires type information
|