UNPKG

1.72 kBMarkdownView Raw
1# `no-meaningless-void-operator`
2
3Disallow the `void` operator except when used to discard a value.
4
5Disallow the `void` operator when its argument is already of type `void` or `undefined`.
6
7## Rule Details
8
9The `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
11This 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
13Examples of code for this rule:
14
15<!--tabs-->
16
17### ❌ Incorrect
18
19```ts
20void (() => {})();
21
22function foo() {}
23void foo();
24```
25
26### ✅ Correct
27
28```ts
29(() => {})();
30
31function foo() {}
32foo(); // nothing to discard
33
34function bar(x: number) {
35 void x; // discarding a number
36 return 2;
37}
38void bar(); // discarding a number
39```
40
41## Options
42
43This 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