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:

配置组 (⚙️)
元数据

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

规则
⚙️
🔧
💭
🧱
💀
@typescript-eslint/adjacent-overload-signatures
要求函数重载签名连续
🎨
@typescript-eslint/array-type
要求对数组一致使用 T[]Array<T>
🎨🔧
@typescript-eslint/await-thenable
禁止等待非 Thenable 的值
💡💭
@typescript-eslint/ban-ts-comment
禁止 @ts-<directive> 注释或要求指令后有描述
💡
@typescript-eslint/ban-tslint-comment
禁止 // tslint:<rule-flag> 注释
🎨🔧
@typescript-eslint/class-literal-property-style
强制类上的字面量以一致的样式公开
🎨💡
@typescript-eslint/class-methods-use-this
强制类方法使用 this
🧱
@typescript-eslint/consistent-generic-constructors
强制在类型注释或构造函数调用的构造函数名称上指定泛型类型参数
🎨🔧
@typescript-eslint/consistent-indexed-object-style
要求或禁止 Record 类型
🎨🔧
💡
@typescript-eslint/consistent-return
要求 return 语句始终或从不指定值
💭🧱
@typescript-eslint/consistent-type-assertions
强制一致使用类型断言
🎨🔧
💡
@typescript-eslint/consistent-type-definitions
强制类型定义一致使用 interfacetype
🎨🔧
@typescript-eslint/consistent-type-exports
强制一致使用类型导出
🔧💭
@typescript-eslint/consistent-type-imports
强制一致使用类型导入
🔧
@typescript-eslint/default-param-last
强制默认参数为最后
🧱
@typescript-eslint/dot-notation
尽可能强制使用点符号
🎨🔧💭🧱
@typescript-eslint/explicit-function-return-type
要求函数和类方法有显式返回类型
@typescript-eslint/explicit-member-accessibility
要求类属性和方法上有显式可访问性修饰符
🔧
💡
@typescript-eslint/explicit-module-boundary-types
Require explicit return and argument types on exported functions' and classes' public class methods
@typescript-eslint/init-declarations
要求或禁止在变量声明中进行初始化
🧱
@typescript-eslint/max-params
强制最大数量函数定义中的参数
🧱
@typescript-eslint/member-ordering
要求一致的成员声明顺序
@typescript-eslint/method-signature-style
强制使用特定方法签名语法
🔧
@typescript-eslint/naming-convention
强制对代码库中的所有内容进行命名约定
💭
@typescript-eslint/no-array-constructor
禁止通用 Array 构造函数
🔧🧱
@typescript-eslint/no-array-delete
不允许在数组值上使用 delete 运算符
💡💭
@typescript-eslint/no-base-to-string
要求 .toString().toLocaleString() 仅在字符串化时提供有用信息的对象上调用
💭
@typescript-eslint/no-confusing-non-null-assertion
禁止在可能造成混淆的位置使用非空断言
🎨💡
@typescript-eslint/no-confusing-void-expression
要求 void 类型的表达式出现在语句位置
🔒🔧
💡
💭
@typescript-eslint/no-deprecated
不允许使用标记为 @deprecated 的代码
🔒💭
@typescript-eslint/no-dupe-class-members
禁止重复的类成员
🧱
@typescript-eslint/no-duplicate-enum-values
禁止重复的枚举成员值
@typescript-eslint/no-duplicate-type-constituents
禁止联合或交集类型的重复成分
🔧💭
@typescript-eslint/no-dynamic-delete
禁止在计算键表达式上使用 delete 运算符
🔒🔧
@typescript-eslint/no-empty-function
禁止空函数
🎨🧱
@typescript-eslint/no-empty-interface
禁止声明空接口
🔧
💡
💀
@typescript-eslint/no-empty-object-type
禁止意外使用 "空对象" 类型
💡
@typescript-eslint/no-explicit-any
禁止 any 类型
🔧
💡
@typescript-eslint/no-extra-non-null-assertion
禁止额外的非空断言
🔧
@typescript-eslint/no-extraneous-class
禁止将类用作命名空间
🔒
@typescript-eslint/no-floating-promises
要求适当处理类似 Promise 的语句
💡💭
@typescript-eslint/no-for-in-array
禁止使用 for-in 循环对数组进行迭代
💭
@typescript-eslint/no-implied-eval
禁止使用类似 eval() 的函数
💭🧱
@typescript-eslint/no-import-type-side-effects
当仅导入时,强制使用顶层导入类型限定符具有内联类型限定符的说明符
🔧
@typescript-eslint/no-inferrable-types
禁止将变量或参数初始化为数字、字符串或布尔值的显式类型声明
🎨🔧
@typescript-eslint/no-invalid-this
禁止在类或类类对象之外使用 this 关键字
🧱
@typescript-eslint/no-invalid-void-type
禁止在泛型或返回类型之外使用 void 类型
🔒
@typescript-eslint/no-loop-func
禁止在循环语句中包含不安全引用的函数声明
🧱
@typescript-eslint/no-loss-of-precision
禁止丢失精度的字面量数字
🧱💀
@typescript-eslint/no-magic-numbers
禁止魔法数字
🧱
@typescript-eslint/no-meaningless-void-operator
禁止使用 void 运算符,除非用于丢弃值
🔒🔧
💡
💭
@typescript-eslint/no-misused-new
强制 newconstructor 的有效定义
@typescript-eslint/no-misused-promises
禁止在未设计用于处理 Promises 的地方使用 Promises
💭
@typescript-eslint/no-misused-spread
禁止使用可能导致意外行为的扩展运算符
🔒💡💭
@typescript-eslint/no-mixed-enums
禁止枚举同时具有数字和字符串成员
🔒💭
@typescript-eslint/no-namespace
禁止 TypeScript 命名空间
@typescript-eslint/no-non-null-asserted-nullish-coalescing
禁止在空值合并运算符的左操作数中使用非空断言
🔒💡
@typescript-eslint/no-non-null-asserted-optional-chain
禁止非空可选链表达式后的断言
💡
@typescript-eslint/no-non-null-assertion
禁止使用 ! 后缀运算符进行非空断言
🔒💡
@typescript-eslint/no-redeclare
不允许变量重新声明
🧱
@typescript-eslint/no-redundant-type-constituents
禁止不执行任何操作或覆盖类型信息的联合和交集成员
💭
@typescript-eslint/no-require-imports
禁止调用 require()
@typescript-eslint/no-restricted-imports
禁止由 import 加载时指定模块
🧱
@typescript-eslint/no-restricted-types
禁止某些类型
🔧
💡
@typescript-eslint/no-shadow
禁止从外部作用域中声明的阴影变量进行变量声明
🧱
@typescript-eslint/no-this-alias
禁止使用 this 别名
@typescript-eslint/no-type-alias
不允许类型别名
💀
@typescript-eslint/no-unnecessary-boolean-literal-compare
禁止对布尔字面量进行不必要的相等比较
🔒🔧💭
@typescript-eslint/no-unnecessary-condition
禁止类型始终为真或始终为假的条件语句
🔒🔧💭
@typescript-eslint/no-unnecessary-parameter-property-assignment
禁止对构造函数属性参数进行不必要的赋值
@typescript-eslint/no-unnecessary-qualifier
不允许不必要的命名空间限定符
🔧💭
@typescript-eslint/no-unnecessary-template-expression
不允许不必要的模板表达式
🔒🔧💭
@typescript-eslint/no-unnecessary-type-arguments
不允许与默认值相等的类型参数
🔒🔧💭
@typescript-eslint/no-unnecessary-type-assertion
禁止不改变表达式类型的类型断言
🔧💭
@typescript-eslint/no-unnecessary-type-constraint
不允许对泛型进行不必要的约束
💡
@typescript-eslint/no-unnecessary-type-parameters
Disallow type parameters that aren't used multiple times
🔒💡💭
@typescript-eslint/no-unsafe-argument
禁止使用类型为 any 的值调用函数
💭
@typescript-eslint/no-unsafe-assignment
禁止将类型为 any 的值分配给变量和属性
💭
@typescript-eslint/no-unsafe-call
禁止调用类型为 any 的值
💭
@typescript-eslint/no-unsafe-declaration-merging
不允许不安全的声明合并
@typescript-eslint/no-unsafe-enum-comparison
禁止将枚举值与非枚举值进行比较
💡💭
@typescript-eslint/no-unsafe-function-type
不允许使用不安全的内置函数类型
@typescript-eslint/no-unsafe-member-access
禁止访问具有 any 类型的值的成员
💭
@typescript-eslint/no-unsafe-return
禁止从函数返回具有 any 类型的值
💭
@typescript-eslint/no-unsafe-type-assertion
不允许缩小类型的类型断言
💭
@typescript-eslint/no-unsafe-unary-minus
要求一元否定取数字
💭
@typescript-eslint/no-unused-expressions
不允许未使用的表达式
🧱
@typescript-eslint/no-unused-vars
不允许未使用的变量
🧱
@typescript-eslint/no-use-before-define
禁止在定义变量之前使用变量
🧱
@typescript-eslint/no-useless-constructor
不允许不必要的构造函数
🔒💡🧱
@typescript-eslint/no-useless-empty-export
Disallow empty exports that don't change anything in a module file
🔧
@typescript-eslint/no-var-requires
禁止除 import 语句之外的 require 语句
💀
@typescript-eslint/no-wrapper-object-types
不允许使用令人困惑的内置原始类封装器
🔧
@typescript-eslint/non-nullable-type-assertion-style
强制非空断言优于显式类型断言
🎨🔧💭
@typescript-eslint/only-throw-error
禁止抛出非 Error 值作为异常
💭🧱
@typescript-eslint/parameter-properties
要求或禁止在类构造函数中进行参数属性
@typescript-eslint/prefer-as-const
强制使用 as const 优于字面量类型
🔧
💡
@typescript-eslint/prefer-destructuring
要求从数组和/或对象解构
🔧💭🧱
@typescript-eslint/prefer-enum-initializers
要求每个枚举成员值都显式初始化
💡
@typescript-eslint/prefer-find
在查找单个结果时,强制使用 Array.prototype.find() 而不是 Array.prototype.filter(),后跟 [0]
🎨💡💭
@typescript-eslint/prefer-for-of
尽可能强制使用 for-of 循环而不是标准 for 循环
🎨
@typescript-eslint/prefer-function-type
强制使用函数类型,而不是带有调用签名的接口
🎨🔧
@typescript-eslint/prefer-includes
强制 includes 方法优于 indexOf 方法
🎨🔧💭
@typescript-eslint/prefer-literal-enum-member
要求所有枚举成员均为字面量值
🔒
@typescript-eslint/prefer-namespace-keyword
要求使用 namespace 关键字而不是 module 关键字来声明自定义 TypeScript 模块
🔧
@typescript-eslint/prefer-nullish-coalescing
强制使用空值合并运算符,而不是逻辑赋值或链接
🎨💡💭
@typescript-eslint/prefer-optional-chain
强制使用简洁的可选链表达式,而不是链式逻辑与、否定逻辑或或空对象
🎨🔧
💡
💭
@typescript-eslint/prefer-promise-reject-errors
要求使用 Error 对象作为 Promise 拒绝原因
💭🧱
@typescript-eslint/prefer-readonly
Require private members to be marked as readonly if they're never modified outside of the constructor
🔧💭
@typescript-eslint/prefer-readonly-parameter-types
要求函数参数类型为 readonly,以防止意外突变输入
💭
@typescript-eslint/prefer-reduce-type-parameter
强制在调用 Array#reduce 时使用类型参数,而不是使用类型断言
🔒🔧💭
@typescript-eslint/prefer-regexp-exec
如果未提供全局标志,则强制 RegExp#exec 优先于 String#match
🎨🔧💭
@typescript-eslint/prefer-return-this-type
强制在仅返回 this 类型时使用 this
🔒🔧💭
@typescript-eslint/prefer-string-starts-ends-with
强制使用 String#startsWithString#endsWith 而不是其他等效的检查子字符串的方法
🎨🔧💭
@typescript-eslint/prefer-ts-expect-error
强制使用 @ts-expect-error 优于 @ts-ignore
🔧💀
@typescript-eslint/promise-function-async
要求任何返回 Promise 的函数或方法都标记为异步
🔧💭
@typescript-eslint/related-getter-setter-pairs
强制 get() 类型应可分配给其等效的 set() 类型
🔒💭
@typescript-eslint/require-array-sort-compare
要求 Array#sortArray#toSorted 调用始终提供 compareFunction
💭
@typescript-eslint/require-await
禁止不返回 promise 且没有 await 表达式的异步函数
💡💭🧱
@typescript-eslint/restrict-plus-operands
要求加法的两个操作数为相同类型,并且为 bigintnumberstring
💭
@typescript-eslint/restrict-template-expressions
强制模板字面量表达式为 string 类型
💭
@typescript-eslint/return-await
强制一致等待返回的 promise
🔧
💡
💭
@typescript-eslint/sort-type-constituents
强制按字母顺序对类型并集/交集的成分进行排序
🔧
💡
💀
@typescript-eslint/strict-boolean-expressions
禁止在布尔表达式中使用某些类型
💡💭
@typescript-eslint/switch-exhaustiveness-check
要求 switch-case 语句详尽无遗
💡💭
@typescript-eslint/triple-slash-reference
禁止某些三斜杠指令,转而使用 ES6 风格的导入声明
@typescript-eslint/typedef
要求在某些地方使用类型注释
@typescript-eslint/unbound-method
强制使用未绑定方法调用其预期范围
💭
@typescript-eslint/unified-signatures
禁止两个可以通过联合或可选/剩余参数合并为一个的重载
🔒
@typescript-eslint/use-unknown-in-catch-callback-variable
强制将 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.