Skip to main content

no-confusing-void-expression

Require expressions of type void to appear in statement position.

🔒

ESLint 配置 中扩展"plugin:@typescript-eslint/strict-type-checked" 可启用此规则。

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复

💡

此规则报告的一些问题可以通过编辑器 建议 手动修复。

💭

该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。

TypeScript 中的 void 指的是要忽略的函数返回。尝试使用 void 类型的值(例如将调用函数的结果存储在变量中)通常是程序员错误的标志。即使正确使用,void 也会误导其他开发者。

¥void in TypeScript refers to a function return that is meant to be ignored. Attempting to use a void-typed value, such as storing the result of a called function in a variable, is often a sign of a programmer error. void can also be misleading for other developers even if used correctly.

当循环索引仅用于从正在迭代的数组中读取时,此规则建议使用 for-of 循​​环。

¥This rule prevents void type expressions from being used in misleading locations such as being assigned to a variable, provided as a function argument, or returned from a function.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-confusing-void-expression": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

// somebody forgot that `alert` doesn't return anything
const response = alert('Are you sure?');
console.log(alert('Are you sure?'));

// it's not obvious whether the chained promise will contain the response (fixable)
promise.then(value => window.postMessage(value));

// it looks like we are returning the result of `console.error` (fixable)
function doSomething() {
if (!somethingToDo) {
return console.error('Nothing to do!');
}

console.log('Doing a thing...');
}
Open in Playground

选项

该规则接受以下选项:

type Options = [
{
/** Whether to ignore "shorthand" `() =>` arrow functions: those without `{ ... }` braces. */
ignoreArrowShorthand?: boolean;
/** Whether to ignore returns that start with the `void` operator. */
ignoreVoidOperator?: boolean;
/** Whether to ignore returns from functions with explicit `void` return types and functions with contextual `void` return types. */
ignoreVoidReturningFunctions?: boolean;
},
];

const defaultOptions: Options = [
{
ignoreArrowShorthand: false,
ignoreVoidOperator: false,
ignoreVoidReturningFunctions: false,
},
];

¥Options

ignoreArrowShorthand

Whether to ignore "shorthand" () => arrow functions: those without { ... } braces. Default: false.

封装每个箭头函数简写表达式可能是不可取的。特别是在使用 Prettier 格式化程序时,它会将此类代码分布在 3 行而不是 1 行。

¥It might be undesirable to wrap every arrow function shorthand expression. Especially when using the Prettier formatter, which spreads such code across 3 lines instead of 1.

启用此选项的其他正确代码示例:

¥Examples of additional correct code with this option enabled:

promise.then(value => window.postMessage(value));
Open in Playground

ignoreVoidOperator

Whether to ignore returns that start with the void operator. Default: false.

最好只使用一些不同的语法来显式标记 void 表达式的令人困惑但有效的用法。此选项允许明确封装在 void 运算符中的 void 表达式。只要其他开发者了解这种代码风格,这就可以帮助避免他们之间的混淆。

¥It might be preferable to only use some distinct syntax to explicitly mark the confusing but valid usage of void expressions. This option allows void expressions which are explicitly wrapped in the void operator. This can help avoid confusion among other developers as long as they are made aware of this code style.

此选项还将常见情况的自动修复更改为使用 void 运算符。它还启用了建议修复,以针对报告的每个问题使用 void 运算符封装 void 表达式。

¥This option also changes the automatic fixes for common cases to use the void operator. It also enables a suggestion fix to wrap the void expression with void operator for every problem reported.

启用此选项的其他正确代码示例:

¥Examples of additional correct code with this option enabled:

// now it's obvious that we don't expect any response
promise.then(value => void window.postMessage(value));

// now it's explicit that we don't want to return anything
function doSomething() {
if (!somethingToDo) {
return void console.error('Nothing to do!');
}

console.log('Doing a thing...');
}

// we are sure that we want to always log `undefined`
console.log(void alert('Hello, world!'));
Open in Playground

ignoreVoidReturningFunctions

Whether to ignore returns from functions with explicit void return types and functions with contextual void return types. Default: false.

一些项目更喜欢允许明确返回 void 的函数返回 void 表达式。这样做可以编写更简洁的函数。

¥Some projects prefer allowing functions that explicitly return void to return void expressions. Doing so allows more writing more succinct functions.

注意

这在技术上是有风险的,因为返回 void 的函数实际上可能会返回类型系统看不到的值。

¥This is technically risky as the void-returning function might actually be returning a value not seen by the type system.

function foo(): void {
return console.log();
}

function onError(callback: () => void): void {
callback();
}

onError(() => console.log('oops'));
Open in Playground

何时不使用它

¥When Not To Use It

可以通过在 IDE 中转到其定义或将鼠标悬停在其上来检查函数的返回类型。如果你不关心在实际代码中明确显示 void 类型,则不要使用此规则。此外,如果你强烈喜欢简洁的编码风格,而不是担心与 void 相关的错误,那么你可以避免此规则。

¥The return type of a function can be inspected by going to its definition or hovering over it in an IDE. If you don't care about being explicit about the void type in actual code then don't use this rule. Also, if you strongly prefer a concise coding style more strongly than any fear of void-related bugs then 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.

'## 资源'