概述
@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 语句始终或从不指定值 | 💭 | 🧱 | |||
强制一致使用类型断言 | 🎨 | 🔧 💡 | |||
强制类型定义一致使用 interface 或 type | 🎨 | 🔧 | |||
强制一致使用类型导出 | 🔧 | 💭 | |||
强制一致使用类型导入 | 🔧 | ||||
强制默认参数为最后 | 🧱 | ||||
尽可能强制使用点符号 | 🎨 | 🔧 | 💭 | 🧱 | |
要求函数和类方法有显式返回类型 | |||||
要求类属性和方法上有显式可访问性修饰符 | 🔧 💡 | ||||
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 运算符,除非用于丢弃值 | 🔒 | 🔧 💡 | 💭 | ||
强制 new 和 constructor 的有效定义 | ✅ | ||||
禁止在未设计用于处理 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#startsWith 和 String#endsWith 而不是其他等效的检查子字符串的方法 | 🎨 | 🔧 | 💭 | ||
强制使用 @ts-expect-error 优于 @ts-ignore | 🔧 | 💀 | |||
要求任何返回 Promise 的函数或方法都标记为异步 | 🔧 | 💭 | |||
强制 get() 类型应可分配给其等效的 set() 类型 | 🔒 | 💭 | |||
要求 Array#sort 和 Array#toSorted 调用始终提供 compareFunction | 💭 | ||||
禁止不返回 promise 且没有 await 表达式的异步函数 | ✅ | 💡 | 💭 | 🧱 | |
要求加法的两个操作数为相 同类型,并且为 bigint、number 或 string | ✅ | 💭 | |||
强制模板字面量表达式为 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自动修复程序。¥
🔧 fixablerefers to whether the rule contains an ESLint--fixauto-fixer. -
💡 has suggestions指的是规则是否包含 ESLint 建议修复程序。¥
💡 has suggestionsrefers 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 informationrefers to whether the rule requires typed linting. -
🧱 extension rule表示该规则是 核心 ESLint 规则 的扩展(参见 扩展规则)。¥
🧱 extension rulemeans that the rule is an extension of an core ESLint rule (see Extension Rules). -
💀 deprecated rule表示该规则不再使用,并将在未来版本中从插件中删除。¥
💀 deprecated rulemeans that the rule should no longer be used and will be removed from the plugin in a future version.