Skip to main content

no-unused-expressions

Disallow unused expressions.

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

🧱

This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.

This rule extends the base no-unused-expressions rule from ESLint core. 它支持 TypeScript 特定的表达式:

¥It supports TypeScript-specific expressions:

  • 将模块声明("use strict" 等)中的指令标记为未使用

    ¥Marks directives in modules declarations ("use strict", etc.) as not unused

  • 如果以下表达式的封装值表达式未使用,则将其标记为未使用:

    ¥Marks the following expressions as unused if their wrapped value expressions are unused:

    • 断言表达式:x as number;, x!;, <number>x;

      ¥Assertion expressions: x as number;, x!;, <number>x;

    • 实例化表达式:Set<number>;

      ¥Instantiation expressions: Set<number>;

尽管类型表达式永远不会产生运行时副作用(即 x!;x; 相同),但它们可用于断言类型以进行测试。

¥Although the type expressions never have runtime side effects (that is, x!; is the same as x;), they can be used to assert types for testing purposes.

示例

¥Examples

Set<number>;
1 as number;
window!;
Open in Playground

选项

See eslint/no-unused-expressions's options.

如何使用

eslint.config.mjs
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"no-unused-expressions": "off",
"@typescript-eslint/no-unused-expressions": "error"
}
});

在线运行试试这个规则 ↗

'## 资源'

Taken with ❤️ from ESLint core.