no-meaningless-void-operator
Disallow the
void
operator except when used to discard a value.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/strict-type-checked"
可启用此规则。
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
此规则报告的一些问题可以通过编辑器 建议 手动修复。
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
TypeScript 中的 void
指的是要忽略的函数返回。void
运算符是一种有用的工具,可以传达程序员丢弃值的意图。例如,建议将其作为抑制 @typescript-eslint/no-floating-promises
而不是将 .catch()
添加到承诺的一种方式。
¥void
in TypeScript refers to a function return that is meant to be ignored.
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
instead of adding .catch()
to a promise.
此规则可帮助作者捕获 API 更改,其中先前值在调用站点被丢弃,但被调用者发生更改,因此不再返回值。当与 no-unused-expressions 结合使用时,它还可以通过确保一致性来帮助代码读者:看起来像 void foo();
的语句总是丢弃返回值,而看起来像 foo();
的语句永远不会丢弃返回值。此规则报告对不提供 void
参数的排序方法的任何调用。
¥This rule helps an authors 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, 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.
This rule reports on any void
operator whose argument is already of type void
or undefined
.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-meaningless-void-operator": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-meaningless-void-operator": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
void (() => {})();
function foo() {}
void foo();
Open in Playground(() => {})();
function foo() {}
foo(); // nothing to discard
function bar(x: number) {
void x; // discarding a number
return 2;
}
void bar(1); // discarding a number
Open in Playground选项
该规则接受以下选项:
type Options = [
{
/** Whether to suggest removing `void` when the argument has type `never`. */
checkNever?: boolean;
},
];
const defaultOptions: Options = [{ checkNever: false }];
¥Options
checkNever
Whether to suggest removing void
when the argument has type never
. Default: false
.
何时不使用它
¥When Not To Use It
如果你不介意项目中有额外的 void
,则可以避免使用此规则。
¥If you don't mind extra void
s in your project, you can avoid this rule.
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.
'## 资源'