Skip to main content

概述

@typescript-eslint/eslint-plugin 包含超过 100 条规则,专门用于检测 TypeScript 代码的最佳实践违规、错误和/或风格问题。下面列出了我们的所有规则。

¥@typescript-eslint/eslint-plugin includes over 100 rules that detect best practice violations, bugs, and/or stylistic issues specifically for TypeScript code. All of our rules are listed below.

提示

我们建议使用 我们预定义的配置 之一来启用大量推荐规则,而不是逐个启用规则。

¥Instead of enabling rules one by one, we recommend using one of our pre-defined configs to enable a large set of recommended rules.

规则

¥Rules

规则按字母顺序列出。你可以选择根据以下类别过滤它们:

¥The rules are listed in alphabetical order. You can optionally filter them based on these categories:

配置组 (⚙️)
元数据

以下将更详细地 解释这些类别。)

规则
⚙️
🔧
💭
🧱
💀

要求函数重载签名连续
🎨

要求对数组一致使用 T[]Array<T>
🎨🔧

禁止等待非 Thenable 的值
💡💭

禁止 @ts-<directive> 注释或要求指令后有描述
💡

禁止 // tslint:<rule-flag> 注释
🎨🔧

强制类上的字面量以一致的样式公开
🎨💡

强制类方法使用 this
🧱

强制在类型注释或构造函数调用的构造函数名称上指定泛型类型参数
🎨🔧

要求或禁止 Record 类型
🎨🔧
💡

要求 return 语句始终或从不指定值
💭🧱

强制一致使用类型断言
🎨🔧
💡

强制类型定义一致使用 interfacetype
🎨🔧

强制一致使用类型导出
🔧💭

强制一致使用类型导入
🔧

强制默认参数为最后
🧱

尽可能强制使用点符号
🎨🔧💭🧱

要求函数和类方法有显式返回类型

要求类属性和方法上有显式可访问性修饰符
🔧
💡

Require explicit return and argument types on exported functions' and classes' public class methods

要求或禁止在变量声明中进行初始化
🧱

强制最大数量函数定义中的参数
🧱

要求一致的成员声明顺序

强制使用特定方法签名语法
🔧

强制对代码库中的所有内容进行命名约定
💭

禁止通用 Array 构造函数
🔧🧱

不允许在数组值上使用 delete 运算符
💡💭

要求 .toString().toLocaleString() 仅在字符串化时提供有用信息的对象上调用
💭

禁止在可能造成混淆的位置使用非空断言
🎨💡

要求 void 类型的表达式出现在语句位置
🔒🔧
💡
💭

不允许使用标记为 @deprecated 的代码
🔒💭

禁止重复的类成员
🧱

禁止重复的枚举成员值

禁止联合或交集类型的重复成分
🔧💭

禁止在计算键表达式上使用 delete 运算符
🔒🔧

禁止空函数
🎨💡🧱

禁止声明空接口
🔧
💡
💀

禁止意外使用 "空对象" 类型
💡

禁止 any 类型
🔧
💡

禁止额外的非空断言
🔧

禁止将类用作命名空间
🔒

要求适当处理类似 Promise 的语句
💡💭

禁止使用 for-in 循环对数组进行迭代
💭

禁止使用类似 eval() 的函数
💭🧱

当仅导入时,强制使用顶层导入类型限定符具有内联类型限定符的说明符
🔧

禁止将变量或参数初始化为数字、字符串或布尔值的显式类型声明
🎨🔧

禁止在类或类类对象之外使用 this 关键字
🧱

禁止在泛型或返回类型之外使用 void 类型
🔒

禁止在循环语句中包含不安全引用的函数声明
🧱

禁止丢失精度的字面量数字
🧱💀

禁止魔法数字
🧱

禁止使用 void 运算符,除非用于丢弃值
🔒🔧
💡
💭

强制 newconstructor 的有效定义

禁止在未设计用于处理 Promises 的地方使用 Promises
💭

禁止使用可能导致意外行为的扩展运算符
🔒💡💭

禁止枚举同时具有数字和字符串成员
🔒💭

禁止 TypeScript 命名空间

禁止在空值合并运算符的左操作数中使用非空断言
🔒💡

禁止非空可选链表达式后的断言
💡

禁止使用 ! 后缀运算符进行非空断言
🔒💡

不允许变量重新声明
🧱

禁止不执行任何操作或覆盖类型信息的联合和交集成员
💭

禁止调用 require()

禁止由 import 加载时指定模块
🧱

禁止某些类型
🔧
💡

禁止从外部作用域中声明的阴影变量进行变量声明
🧱

禁止使用 this 别名

不允许类型别名
💀

禁止对布尔字面量进行不必要的相等比较
🔒🔧💭

禁止类型始终为真或始终为假的条件语句
🔒💡💭

禁止对构造函数属性参数进行不必要的赋值

不允许不必要的命名空间限定符
🔧💭

不允许不必要的模板表达式
🔒🔧💭

不允许与默认值相等的类型参数
🔒🔧💭

禁止不改变表达式类型的类型断言
🔧💭

不允许对泛型进行不必要的约束
💡

当转换习惯用法不改变表达式的类型或值时,禁止使用它们。
🔒💡💭

Disallow type parameters that aren't used multiple times
🔒💡💭

禁止使用类型为 any 的值调用函数
💭

禁止将类型为 any 的值分配给变量和属性
💭

禁止调用类型为 any 的值
💭

不允许不安全的声明合并

禁止将枚举值与非枚举值进行比较
💡💭

不允许使用不安全的内置函数类型

禁止访问具有 any 类型的值的成员
💭

禁止从函数返回具有 any 类型的值
💭

不允许缩小类型的类型断言
💭

要求一元否定取数字
💭

不允许未使用的表达式
🧱

不允许未使用的变量
🧱

禁止在定义变量之前使用变量
🧱

不允许不必要的构造函数
🔒💡🧱

Disallow empty exports that don't change anything in a module file
🔧

禁止除 import 语句之外的 require 语句
💀

不允许使用令人困惑的内置原始类封装器
🔧

强制非空断言优于显式类型断言
🎨🔧💭

禁止抛出非 Error 值作为异常
💭🧱

要求或禁止在类构造函数中进行参数属性

强制使用 as const 优于字面量类型
🔧
💡

要求从数组和/或对象解构
🔧💭🧱

要求每个枚举成员值都显式初始化
💡

在查找单个结果时,强制使用 Array.prototype.find() 而不是 Array.prototype.filter(),后跟 [0]
🎨💡💭

尽可能强制使用 for-of 循环而不是标准 for 循环
🎨

强制使用函数类型,而不是带有调用签名的接口
🎨🔧

强制 includes 方法优于 indexOf 方法
🎨🔧💭

要求所有枚举成员均为字面量值
🔒

要求使用 namespace 关键字而不是 module 关键字来声明自定义 TypeScript 模块
🔧

强制使用空值合并运算符,而不是逻辑赋值或链接
🎨💡💭

强制使用简洁的可选链表达式,而不是链式逻辑与、否定逻辑或或空对象
🎨🔧
💡
💭

要求使用 Error 对象作为 Promise 拒绝原因
💭🧱

Require private members to be marked as readonly if they're never modified outside of the constructor
🔧💭

要求函数参数类型为 readonly,以防止意外突变输入
💭

强制在调用 Array#reduce 时使用类型参数,而不是使用类型断言
🔒🔧💭

如果未提供全局标志,则强制 RegExp#exec 优先于 String#match
🎨🔧💭

强制在仅返回 this 类型时使用 this
🔒🔧💭

强制使用 String#startsWithString#endsWith 而不是其他等效的检查子字符串的方法
🎨🔧💭

强制使用 @ts-expect-error 优于 @ts-ignore
🔧💀

要求任何返回 Promise 的函数或方法都标记为异步
🔧💭

强制 get() 类型应可分配给其等效的 set() 类型
🔒💭

要求 Array#sortArray#toSorted 调用始终提供 compareFunction
💭

禁止不返回 promise 且没有 await 表达式的异步函数
💡💭🧱

要求加法的两个操作数为相同类型,并且为 bigintnumberstring
💭

强制模板字面量表达式为 string 类型
💭

强制一致等待返回的 promise
🔒🔧
💡
💭

强制按字母顺序对类型并集/交集的成分进行排序
🔧
💡
💀

禁止在布尔表达式中使用某些类型
💡💭

要求 switch-case 语句详尽无遗
💡💭

禁止某些三斜杠指令,转而使用 ES6 风格的导入声明

要求在某些地方使用类型注释
💀

强制使用未绑定方法调用其预期范围
💭

禁止两个可以通过联合或可选/剩余参数合并为一个的重载
🔒

强制将 Promise 拒绝回调中的参数键入为 unknown
🔒💡💭

过滤

¥Filtering

配置组 (⚙️)

¥Config Group (⚙️)

"配置组" 指的是包含规则的 预定义配置。从配置预设扩展允许一次启用大量推荐规则。

¥"Config Group" refers to the pre-defined config that includes the rule. Extending from a configuration preset allow for enabling a large set of recommended rules all at once.

元数据

¥Metadata

  • 🔧 fixable 指的是规则是否包含 ESLint --fix 自动修复程序

    ¥🔧 fixable refers to whether the rule contains an ESLint --fix auto-fixer.

  • 💡 has suggestions 指的是规则是否包含 ESLint 建议修复程序。

    ¥💡 has suggestions refers to whether the rule contains an ESLint suggestion fixer.

    • 有时,使用自动修复程序自动修复代码并不安全。但在这些情况下,我们通常可以很好地猜测正确的修复应该是什么,并且我们可以将其作为建议提供给开发者。

      ¥Sometimes, it is not safe to automatically fix the code with an auto-fixer. But in these cases, we often have a good guess of what the correct fix should be, and we can provide it as a suggestion to the developer.

  • 💭 requires type information 指的是规则是否需要 类型化的 linting

    ¥💭 requires type information refers to whether the rule requires typed linting.

  • 🧱 extension rule 表示该规则是 核心 ESLint 规则 的扩展(参见 扩展规则)。

    ¥🧱 extension rule means that the rule is an extension of an core ESLint rule (see Extension Rules).

  • 💀 deprecated rule 表示该规则不再使用,并将在未来版本中从插件中删除。

    ¥💀 deprecated rule means that the rule should no longer be used and will be removed from the plugin in a future version.

扩展规则

¥Extension Rules

一些核心 ESLint 规则不支持 TypeScript 语法:它们要么崩溃,要么忽略语法,要么错误地报告语法。在这些情况下,我们创建所谓的 "扩展规则":我们的插件中的规则具有相同的功能,但也支持 TypeScript。

¥Some core ESLint rules do not support TypeScript syntax: either they crash, ignore the syntax, or falsely report against it. In these cases, we create what we call an "extension rule": a rule within our plugin that has the same functionality, but also supports TypeScript.

扩展规则通常完全取代 ESLint 核心的基本规则。如果在你扩展的配置中启用了基本规则,你需要禁用基本规则:

¥Extension rules generally completely replace the base rule from ESLint core. If the base rule is enabled in a config you extend from, you'll need to disable the base rule:

module.exports = {
extends: ['eslint:recommended'],
rules: {
// Note: you must disable the base rule as it can report incorrect errors
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
},
};

此页面中的 搜索 🧱 extension rule 查看所有扩展规则。

¥Search for 🧱 extension rules in this page to see all extension rules.

冻结规则

¥Frozen Rules

当规则功能完备时,它们会被标记为冻结(在文档中用❄️表示)。这适用于完整的独立规则,以及底层核心 ESLint 规则已冻结的 扩展规则。此后,我们希望用户在发现未涵盖的边缘情况时使用 禁用注释

¥When rules are feature complete, they are marked as frozen (indicated with ❄️ in the documentation). This applies to standalone rules that are complete, as well as extension rules whose underlying core ESLint rules are frozen. After that point, we expect users to use disable comments when they find an edge case that isn’t covered.

规则冻结时,这意味着:

¥When a rule is frozen, it means:

  • 错误修复:我们仍将修复已确认的错误。

    ¥Bug fixes: We will still fix confirmed bugs.

  • 新的 ECMAScript 特性:我们将确保与新的 ECMAScript 特性兼容,这意味着该规则不会破坏新的语法。

    ¥New ECMAScript features: We will ensure compatibility with new ECMAScript features, meaning the rule will not break on new syntax.

  • TypeScript 支持:我们将确保与 TypeScript 语法兼容,这意味着该规则不会破坏 TypeScript 语法,并且违反规则的行为也适用于 TypeScript。

    ¥TypeScript support: We will ensure compatibility with TypeScript syntax, meaning the rule will not break on TypeScript syntax and violations are appropriate for TypeScript.

  • 新选项:除非某个选项是修复错误或支持新添加的 ECMAScript 特性的唯一方法,否则我们不会添加任何新选项。

    ¥New options: We will not add any new options unless an option is the only way to fix a bug or support a newly-added ECMAScript feature.

如果你发现冻结规则经过更改后效果更佳,我们建议你复制规则源代码并进行修改以满足你的需求。

¥If you find that a frozen rule would work better for you with a change, we recommend copying the rule source code and modifying it to fit your needs.