跳到主要内容

TypeScript 规则

eslint-config-airbnb-typescript 是一个针对 TypeScript 的 Airbnb 风格扩展,它允许在 TypeScript 项目中使用 Airbnb 的规则。它通常需要与 @typescript-eslint/parser@typescript-eslint/eslint-plugin 一起使用。

// .eslintrc.js

module.exports = {
extends: [
// ...
'airbnb-typescript',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
],
};
注意
  • 截止至目前,eslint-config-airbnb-typescript 仍不支持 ESLint 9.x 版本,使用时请注意。使用时请使用 ESLint 8.x 版本,耐心等待 eslint-config-airbnb-typescript 升级。
  • 由于 eslint-config-airbnb-typescript 需要搭配 eslint-config-airbnbeslint-config-airbnb-base 使用。
  • 由于 eslint-config-airbnb-typescript 需要搭配 @typescript-eslint/parser@typescript-eslint/eslint-plugin 使用,截止目前仅支持 7.x 版本。
  • 由于 eslint-config-airbnb-typescript 目前是只读状态,后续需要持续关注项目情况,或寻求其他替代项目。

基础规则

规则名称错误级别配置选项描述
@typescript-eslint/brace-styleerror["1tbs", { allowSingleLine: true }]强制大括号样式使用1tbs风格,允许单行形式
@typescript-eslint/naming-conventionerror[{selector: "variable", format: ["camelCase", "PascalCase", "UPPER_CASE"]}, {selector: "function", format: ["camelCase", "PascalCase"]}, {selector: "typeLike", format: ["PascalCase"]}]强制命名约定规范
@typescript-eslint/comma-dangleerror[{arrays: "always-multiline", objects: "always-multiline", imports: "always-multiline", exports: "always-multiline", functions: "always-multiline", enums: "always-multiline", generics: "always-multiline", tuples: "always-multiline"}]强制在多行结构中使用尾随逗号
@typescript-eslint/comma-spacingerror[{ before: false, after: true }]控制逗号前后空格
@typescript-eslint/default-param-lasterror-强制默认参数放在最后
@typescript-eslint/dot-notationerror[{ allowKeywords: true }]强制使用点号表示法访问属性
@typescript-eslint/func-call-spacingerror["never"]禁止函数名与调用括号之间的空格
@typescript-eslint/indenterror[2, { SwitchCase: 1, VariableDeclarator: 1, outerIIFEBody: 1, FunctionDeclaration: { parameters: 1, body: 1 }, FunctionExpression: { parameters: 1, body: 1 }, CallExpression: { arguments: 1 }, ArrayExpression: 1, ObjectExpression: 1, ImportDeclaration: 1, flatTernaryExpressions: false, ignoredNodes: ["JSXElement..."] }]强制缩进为2个空格
@typescript-eslint/keyword-spacingerror[{ before: true, after: true, overrides: { return: { after: true }, throw: { after: true }, case: { after: true } }]强制关键字周围空格一致性
@typescript-eslint/lines-between-class-memberserror["always", { exceptAfterSingleLine: false }]强制类成员之间有空行
@typescript-eslint/no-array-constructorerror-禁止使用Array构造函数
@typescript-eslint/no-dupe-class-memberserror-禁止重复类成员
@typescript-eslint/no-empty-functionerror[{ allow: ["arrowFunctions", "functions", "methods"] }]禁止空函数
@typescript-eslint/no-extra-semierror-禁止不必要的分号
@typescript-eslint/no-implied-evalerror-禁止隐式eval用法
@typescript-eslint/no-loss-of-precisionerror-禁止精度丢失的数字字面量
@typescript-eslint/no-loop-funcerror-禁止在循环中创建函数
@typescript-eslint/no-redeclareerror-禁止重复声明变量
@typescript-eslint/no-shadowerror-禁止变量声明覆盖外层作用域
@typescript-eslint/space-before-blockserror-强制块之前的空格
@typescript-eslint/no-throw-literalerror-禁止抛出非Error对象
@typescript-eslint/no-unused-expressionserror[{ allowShortCircuit: false, allowTernary: false, allowTaggedTemplates: false }]禁止未使用的表达式
@typescript-eslint/no-unused-varserror[{ vars: "all", args: "after-used", ignoreRestSiblings: true }]禁止未使用的变量
@typescript-eslint/no-use-before-defineerror[{ functions: true, classes: true, variables: true }]禁止在定义前使用
@typescript-eslint/no-useless-constructorerror-禁止不必要的构造函数
@typescript-eslint/quoteserror["single", { avoidEscape: true }]强制使用单引号
@typescript-eslint/semierror["always"]强制使用分号
@typescript-eslint/space-before-function-parenerror[{ anonymous: "always", named: "never", asyncArrow: "always" }]强制函数括号前的空格
@typescript-eslint/return-awaiterror["in-try-catch"]强制在try-catch中使用return await
@typescript-eslint/space-infix-opserror-强制操作符周围空格
@typescript-eslint/object-curly-spacingerror["always"]强制对象大括号内空格
import/extensionserror["ignorePackages", { js: "never", mjs: "never", jsx: "never", ts: "never", tsx: "never" }]禁止文件扩展名在导入语句中使用
import/no-extraneous-dependencieserror[{ devDependencies: [...] }]禁止无关的依赖引入

原生 ESLint 规则覆盖说明

以下 ESLint 规则,已被 @typescript-eslint 替代:

规则名称错误级别说明
brace-styleoff@typescript-eslint/brace-style 替代
camelcaseoff@typescript-eslint/naming-convention 替代
comma-dangleoff@typescript-eslint/comma-dangle 替代
comma-spacingoff@typescript-eslint/comma-spacing 替代
default-param-lastoff@typescript-eslint/default-param-last 替代
dot-notationoff@typescript-eslint/dot-notation 替代
func-call-spacingoff@typescript-eslint/func-call-spacing 替代
indentoff@typescript-eslint/indent 替代
keyword-spacingoff@typescript-eslint/keyword-spacing 替代
lines-between-class-membersoff@typescript-eslint/lines-between-class-members 替代
no-array-constructoroff@typescript-eslint/no-array-constructor 替代
no-dupe-class-membersoff@typescript-eslint/no-dupe-class-members 替代
no-empty-functionoff@typescript-eslint/no-empty-function 替代
no-extra-parensoff@typescript-eslint/no-extra-parens 替代
no-extra-semioff@typescript-eslint/no-extra-semi 替代
no-implied-evaloff@typescript-eslint/no-implied-eval 替代
no-new-funcoff@typescript-eslint/no-implied-eval 替代
no-loss-of-precisionoff@typescript-eslint/no-loss-of-precision 替代
no-loop-funcoff@typescript-eslint/no-loop-func 替代
no-magic-numbersoff@typescript-eslint/no-magic-numbers 替代
no-redeclareoff@typescript-eslint/no-redeclare 替代
no-shadowoff@typescript-eslint/no-shadow 替代
space-before-blocksoff@typescript-eslint/space-before-blocks 替代
no-throw-literaloff@typescript-eslint/no-throw-literal 替代
no-unused-expressionsoff@typescript-eslint/no-unused-expressions 替代
no-unused-varsoff@typescript-eslint/no-unused-vars 替代
no-use-before-defineoff@typescript-eslint/no-use-before-define 替代
no-useless-constructoroff@typescript-eslint/no-useless-constructor 替代
quotesoff@typescript-eslint/quotes 替代
semioff@typescript-eslint/semi 替代
space-before-function-parenoff@typescript-eslint/space-before-function-paren 替代
require-awaitoff@typescript-eslint/require-await 替代
no-return-awaitoff@typescript-eslint/return-await 替代
space-infix-opsoff@typescript-eslint/space-infix-ops 替代
object-curly-spacingoff@typescript-eslint/object-curly-spacing 替代

以下 ESLint 规则,在 *.ts / *.tsx 已被关闭:

规则名称错误级别描述
constructor-superoff禁用构造函数中 super() 调用检查(由 TypeScript 编译器处理)
getter-returnoff禁用 Getter 返回值检查(TS 类型系统已覆盖)
no-const-assignoff禁用 const 变量重新赋值检查(TS 编译器直接报错)
no-dupe-argsoff禁用函数参数重复检查(TS 类型系统已覆盖)
no-dupe-class-membersoff禁用类成员重复检查(由 @typescript-eslint/no-dupe-class-members 替代)
no-dupe-keysoff禁用对象属性重复检查(TS 类型系统已覆盖)
no-func-assignoff禁用函数重新赋值检查(TS 编译器直接报错)
no-import-assignoff禁用 import 导入值重新赋值检查(TS 类型系统已限制)
no-new-symboloff禁用 Symbol 构造函数检查(TS 类型系统已覆盖)
no-obj-callsoff禁用全局对象函数调用检查(如 Math(),TS 编译器报错)
no-redeclareoff禁用变量重复声明检查(由 @typescript-eslint/no-redeclare 替代)
no-setter-returnoff禁用 Setter 返回值检查(TS 类型系统已覆盖)
no-this-before-superoff禁用 super() 前使用 this 检查(TS 编译器直接报错)
no-undefoff禁用未定义变量检查(TS 类型系统替代此功能)
no-unreachableoff禁用不可达代码检查(TS 编译器直接报错)
no-unsafe-negationoff禁用不安全取反操作检查(TS 类型系统已覆盖)
valid-typeofoff禁用 typeof 校验(TS 类型系统替代此功能)
import/namedoff禁用具名导入校验(TS 类型系统已覆盖)
import/no-named-as-default-memberoff禁用默认导入成员的校验(TS 类型系统处理更精确)
import/no-unresolvedoff禁用模块路径解析校验(由 TS 编译器处理)

plugin:@typescript-eslint/recommended-type-checked 提供了 TypeScript 推荐的规则,并且这些规则需要类型信息才能工作。它会进行更深层次的分析,例如检查变量类型、函数重载等,从而提供更强大的类型安全保障。

规则名称错误级别配置选项描述
@typescript-eslint/await-thenableerror-禁止对非 Promise(thenable)值使用 await
@typescript-eslint/ban-ts-commenterror默认配置禁止使用 @ts-<directive> 注释
@typescript-eslint/ban-typeserror默认配置禁止使用特定 TypeScript 类型(如 ObjectString 等)
no-array-constructoroff-原生 ESLint 规则:禁止使用 Array 构造函数
@typescript-eslint/no-array-constructorerror-TypeScript 版本:禁止使用 Array 构造函数
@typescript-eslint/no-base-to-stringerror-强制要求对象必须实现有效的 toString() 方法
@typescript-eslint/no-duplicate-enum-valueserror-禁止枚举成员有重复值
@typescript-eslint/no-duplicate-type-constituentserror-禁止类型中出现重复的组成部分(如联合类型中的重复子类型)
@typescript-eslint/no-explicit-anyerror-禁止显式使用 any 类型
@typescript-eslint/no-extra-non-null-assertionerror-禁止不必要的非空断言(如 !! 或连续 !
@typescript-eslint/no-floating-promiseserror-禁止未处理的 Promise(需显式处理或 await
@typescript-eslint/no-for-in-arrayerror-禁止对数组使用 for-in 循环(建议用 for-of
no-implied-evaloff-原生 ESLint 规则:禁止隐式 eval(如 setTimeout('code')
@typescript-eslint/no-implied-evalerror-TypeScript 版本:禁止隐式 eval
no-loss-of-precisionoff-原生 ESLint 规则:禁止数字精度丢失
@typescript-eslint/no-loss-of-precisionerror-TypeScript 版本:禁止数字精度丢失
@typescript-eslint/no-misused-newerror-禁止误用 new 操作符(如接口定义中的构造函数)
@typescript-eslint/no-misused-promiseserror-禁止在非 Promise 上下文中使用 Promise(如 if (promise)
@typescript-eslint/no-namespaceerror-禁止使用 TypeScript 的命名空间(namespace
@typescript-eslint/no-non-null-asserted-optional-chainerror-禁止在可选链后使用非空断言(如 obj?.prop!
@typescript-eslint/no-redundant-type-constituentserror-禁止类型中出现冗余的组成部分(如联合类型中的 string | string
@typescript-eslint/no-this-aliaserror-禁止将 this 赋值给变量(需使用箭头函数或类属性)
@typescript-eslint/no-unnecessary-type-assertionerror-禁止不必要的类型断言(如 x as number 当类型已明确时)
@typescript-eslint/no-unnecessary-type-constrainterror-禁止泛型约束中的不必要类型(如 <T extends unknown>
@typescript-eslint/no-unsafe-argumenterror-禁止将不安全的值(如 any)作为函数参数传递
@typescript-eslint/no-unsafe-assignmenterror-禁止将不安全的值(如 any)赋值给变量
@typescript-eslint/no-unsafe-callerror-禁止调用类型不安全的函数(如 any 类型的函数)
@typescript-eslint/no-unsafe-declaration-mergingerror-禁止不安全的声明合并(如接口与类合并导致类型冲突)
@typescript-eslint/no-unsafe-enum-comparisonerror-禁止枚举与非枚举值的比较(避免类型错误)
@typescript-eslint/no-unsafe-member-accesserror-禁止访问 any 类型对象的成员(需明确类型)
@typescript-eslint/no-unsafe-returnerror-禁止返回 any 类型或未知类型的值
no-unused-varsoff-原生 ESLint 规则:禁止未使用的变量
@typescript-eslint/no-unused-varserror-TypeScript 版本:禁止未使用的变量
@typescript-eslint/no-var-requireserror-禁止使用 require() 导入(需用 ES6 import
@typescript-eslint/prefer-as-consterror-强制使用 as const 替代类型断言(保留字面量类型)
require-awaitoff-原生 ESLint 规则:要求异步函数内有 await
@typescript-eslint/require-awaiterror-TypeScript 版本:要求异步函数内有 await
@typescript-eslint/restrict-plus-operandserror-限制 + 操作符的操作数类型(必须为数字或字符串)
@typescript-eslint/restrict-template-expressionserror-限制模板字符串中的表达式类型(需可安全转换为字符串)
@typescript-eslint/triple-slash-referenceerror-禁止使用三斜线引用指令(/// <reference />
@typescript-eslint/unbound-methoderror-禁止未绑定的方法调用(需先绑定 this 上下文)

stylistic-type-checked

plugin:@typescript-eslint/strict-type-checked 是一个更严格的 TypeScript 规则集,它在 recommended-type-checked 的基础上增加了更多的规则,以强制执行更严格的类型检查和最佳实践,有助于防止潜在的类型相关错误。

规则名称错误级别配置选项描述
@typescript-eslint/await-thenableerror-禁止 await 非 Promise 的值
@typescript-eslint/ban-ts-commenterror{ minimumDescriptionLength: 10 }禁止使用 TypeScript 注释指令(如 @ts-ignore
@typescript-eslint/ban-typeserror-禁止使用特定危险类型(如 Object/{}
@typescript-eslint/no-array-constructorerror-禁止使用 Array 构造函数
@typescript-eslint/no-array-deleteerror-禁止使用 delete 操作符删除数组元素
@typescript-eslint/no-base-to-stringerror-强制对象必须有有意义的 toString() 方法
@typescript-eslint/no-confusing-void-expressionerror-禁止在返回 void 的地方误用表达式
@typescript-eslint/no-duplicate-enum-valueserror-禁止枚举成员有重复值
@typescript-eslint/no-duplicate-type-constituentserror-禁止类型中出现重复的组成部分
@typescript-eslint/no-dynamic-deleteerror-禁止使用动态计算的键进行 delete 操作
@typescript-eslint/no-explicit-anyerror-禁止显式使用 any 类型
@typescript-eslint/no-extra-non-null-assertionerror-禁止多余的非空断言(!!
@typescript-eslint/no-extraneous-classerror-禁止冗余的类定义
@typescript-eslint/no-floating-promiseserror-禁止未处理的 Promise
@typescript-eslint/no-for-in-arrayerror-禁止在数组上使用 for-in 循环
@typescript-eslint/no-implied-evalerror-禁止隐式的 eval() 用法
@typescript-eslint/no-invalid-void-typeerror-禁止无效的 void 类型使用
@typescript-eslint/no-loss-of-precisionerror-禁止数字字面量精度丢失
@typescript-eslint/no-meaningless-void-operatorerror-禁止无意义的 void 操作符
@typescript-eslint/no-misused-newerror-禁止误用 new 关键字
@typescript-eslint/no-misused-promiseserror-禁止误用 Promise
@typescript-eslint/no-mixed-enumserror-禁止混合类型的枚举成员
@typescript-eslint/no-namespaceerror-禁止使用 namespace 自定义模块
@typescript-eslint/no-non-null-asserted-nullish-coalescingerror-禁止在空值合并操作符后使用非空断言
@typescript-eslint/no-non-null-asserted-optional-chainerror-禁止在可选链后使用非空断言
@typescript-eslint/no-non-null-assertionerror-禁止非空断言(!
@typescript-eslint/no-redundant-type-constituentserror-禁止类型中的冗余组成部分
@typescript-eslint/no-this-aliaserror-禁止将 this 赋值给变量
@typescript-eslint/no-unnecessary-boolean-literal-compareerror-禁止不必要的布尔字面量比较
@typescript-eslint/no-unnecessary-conditionerror-禁止不必要的条件判断
@typescript-eslint/no-unnecessary-template-expressionerror-禁止模板字符串中的冗余表达式
@typescript-eslint/no-unnecessary-type-argumentserror-禁止不必要的泛型类型参数
@typescript-eslint/no-unnecessary-type-assertionerror-禁止不必要的类型断言
@typescript-eslint/no-unnecessary-type-constrainterror-禁止不必要的类型约束
@typescript-eslint/no-unsafe-argumenterror-禁止不安全的函数参数传递
@typescript-eslint/no-unsafe-assignmenterror-禁止不安全的变量赋值
@typescript-eslint/no-unsafe-callerror-禁止不安全的函数调用
@typescript-eslint/no-unsafe-declaration-mergingerror-禁止不安全的声明合并
@typescript-eslint/no-unsafe-enum-comparisonerror-禁止不安全的枚举比较
@typescript-eslint/no-unsafe-member-accesserror-禁止不安全的成员访问
@typescript-eslint/no-unsafe-returnerror-禁止不安全的返回值
@typescript-eslint/no-unused-varserror-禁止未使用的变量
@typescript-eslint/no-useless-constructorerror-禁止冗余的构造函数
@typescript-eslint/no-var-requireserror-禁止使用 require() 导入模块
@typescript-eslint/only-throw-errorerror-强制 throw 必须是 Error 实例
@typescript-eslint/prefer-as-consterror-推荐使用 as const 断言字面量类型
@typescript-eslint/prefer-includeserror-推荐使用 .includes() 替代 indexOf 检查存在性
@typescript-eslint/prefer-literal-enum-membererror-推荐枚举成员使用字面量值
@typescript-eslint/prefer-promise-reject-errorserror-推荐 Promise.reject() 必须使用 Error 实例
@typescript-eslint/prefer-reduce-type-parametererror-推荐使用 Array.reduce 的类型参数
@typescript-eslint/prefer-return-this-typeerror-推荐方法返回 this 类型
@typescript-eslint/require-awaiterror-强制异步函数必须有 await
@typescript-eslint/restrict-plus-operandserror{ allowAny: false, allowBoolean: false, allowNullish: false, allowNumberAndString: false, allowRegExp: false }限制 + 操作符的操作数类型
@typescript-eslint/restrict-template-expressionserror{ allowAny: false, allowBoolean: false, allowNullish: false, allowNumber: false, allowRegExp: false, allowNever: false }限制模板字符串中的表达式类型
@typescript-eslint/triple-slash-referenceerror-禁止使用三斜线引用指令
@typescript-eslint/unbound-methoderror-禁止未绑定的方法调用
@typescript-eslint/unified-signatureserror-强制合并重载函数签名
@typescript-eslint/use-unknown-in-catch-callback-variableerror-强制 catch 变量类型为 unknown

原生 ESLint 规则覆盖说明

以下 ESLint 规则,已被 @typescript-eslint 替代:

规则名称错误级别说明
no-array-constructoroff@typescript-eslint/no-array-constructor 替代
no-implied-evaloff@typescript-eslint/no-implied-eval 替代
no-loss-of-precisionoff@typescript-eslint/no-loss-of-precision 替代
no-throw-literaloff@typescript-eslint/only-throw-error 替代
no-unused-varsoff@typescript-eslint/no-unused-vars 替代
no-useless-constructoroff@typescript-eslint/no-useless-constructor 替代
prefer-promise-reject-errorsoff@typescript-eslint/prefer-promise-reject-errors 替代
require-awaitoff@typescript-eslint/require-await 替代