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-airbnb或eslint-config-airbnb-base使用。 - 由于
eslint-config-airbnb-typescript需要搭配@typescript-eslint/parser和@typescript-eslint/eslint-plugin使用,截止目前仅支持 7.x 版本。 - 由于
eslint-config-airbnb-typescript目前是只读状态,后续需要持续关注项目情况,或寻求其他替代项目。
基础规则
| 规则名称 | 错误级别 | 配置选项 | 描述 |
|---|---|---|---|
| @typescript-eslint/brace-style | error | ["1tbs", { allowSingleLine: true }] | 强制大括号样式使用1tbs风格,允许单行形式 |
| @typescript-eslint/naming-convention | error | [{selector: "variable", format: ["camelCase", "PascalCase", "UPPER_CASE"]}, {selector: "function", format: ["camelCase", "PascalCase"]}, {selector: "typeLike", format: ["PascalCase"]}] | 强制命名约定规范 |
| @typescript-eslint/comma-dangle | error | [{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-spacing | error | [{ before: false, after: true }] | 控制逗号前后空格 |
| @typescript-eslint/default-param-last | error | - | 强制默认参数放在最后 |
| @typescript-eslint/dot-notation | error | [{ allowKeywords: true }] | 强制使用点号表示法访问属性 |
| @typescript-eslint/func-call-spacing | error | ["never"] | 禁止函数名与调用括号之间的空格 |
| @typescript-eslint/indent | error | [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-spacing | error | [{ before: true, after: true, overrides: { return: { after: true }, throw: { after: true }, case: { after: true } }] | 强制关键字周围空格一致性 |
| @typescript-eslint/lines-between-class-members | error | ["always", { exceptAfterSingleLine: false }] | 强制类成员之间有空行 |
| @typescript-eslint/no-array-constructor | error | - | 禁止使用Array构造函数 |
| @typescript-eslint/no-dupe-class-members | error | - | 禁止重复类成员 |
| @typescript-eslint/no-empty-function | error | [{ allow: ["arrowFunctions", "functions", "methods"] }] | 禁止空函数 |
| @typescript-eslint/no-extra-semi | error | - | 禁止不必要的分号 |
| @typescript-eslint/no-implied-eval | error | - | 禁止隐式eval用法 |
| @typescript-eslint/no-loss-of-precision | error | - | 禁止精度丢失的数字字面量 |
| @typescript-eslint/no-loop-func | error | - | 禁止在循环中创建函数 |
| @typescript-eslint/no-redeclare | error | - | 禁止重复声明变量 |
| @typescript-eslint/no-shadow | error | - | 禁止变量声明覆盖外层作用域 |
| @typescript-eslint/space-before-blocks | error | - | 强制块之前的空格 |
| @typescript-eslint/no-throw-literal | error | - | 禁止抛出非Error对象 |
| @typescript-eslint/no-unused-expressions | error | [{ allowShortCircuit: false, allowTernary: false, allowTaggedTemplates: false }] | 禁止未使用的表达式 |
| @typescript-eslint/no-unused-vars | error | [{ vars: "all", args: "after-used", ignoreRestSiblings: true }] | 禁止未使用的变量 |
| @typescript-eslint/no-use-before-define | error | [{ functions: true, classes: true, variables: true }] | 禁止在定义前使用 |
| @typescript-eslint/no-useless-constructor | error | - | 禁止不必要的构造函数 |
| @typescript-eslint/quotes | error | ["single", { avoidEscape: true }] | 强制使用单引号 |
| @typescript-eslint/semi | error | ["always"] | 强制使用分号 |
| @typescript-eslint/space-before-function-paren | error | [{ anonymous: "always", named: "never", asyncArrow: "always" }] | 强制函数括号前的空格 |
| @typescript-eslint/return-await | error | ["in-try-catch"] | 强制在try-catch中使用return await |
| @typescript-eslint/space-infix-ops | error | - | 强制操作符周围空格 |
| @typescript-eslint/object-curly-spacing | error | ["always"] | 强制对象大括号内空格 |
| import/extensions | error | ["ignorePackages", { js: "never", mjs: "never", jsx: "never", ts: "never", tsx: "never" }] | 禁止文件扩展名在导入语句中使用 |
| import/no-extraneous-dependencies | error | [{ devDependencies: [...] }] | 禁止无关的依赖引入 |
原生 ESLint 规则覆盖说明
以下 ESLint 规则,已被 @typescript-eslint 替代:
| 规则名称 | 错误级别 | 说明 |
|---|---|---|
brace-style | off | 被 @typescript-eslint/brace-style 替代 |
camelcase | off | 被 @typescript-eslint/naming-convention 替代 |
comma-dangle | off | 被 @typescript-eslint/comma-dangle 替代 |
comma-spacing | off | 被 @typescript-eslint/comma-spacing 替代 |
default-param-last | off | 被 @typescript-eslint/default-param-last 替代 |
dot-notation | off | 被 @typescript-eslint/dot-notation 替代 |
func-call-spacing | off | 被 @typescript-eslint/func-call-spacing 替代 |
indent | off | 被 @typescript-eslint/indent 替代 |
keyword-spacing | off | 被 @typescript-eslint/keyword-spacing 替代 |
lines-between-class-members | off | 被 @typescript-eslint/lines-between-class-members 替代 |
no-array-constructor | off | 被 @typescript-eslint/no-array-constructor 替代 |
no-dupe-class-members | off | 被 @typescript-eslint/no-dupe-class-members 替代 |
no-empty-function | off | 被 @typescript-eslint/no-empty-function 替代 |
no-extra-parens | off | 被 @typescript-eslint/no-extra-parens 替代 |
no-extra-semi | off | 被 @typescript-eslint/no-extra-semi 替代 |
no-implied-eval | off | 被 @typescript-eslint/no-implied-eval 替代 |
no-new-func | off | 被 @typescript-eslint/no-implied-eval 替代 |
no-loss-of-precision | off | 被 @typescript-eslint/no-loss-of-precision 替代 |
no-loop-func | off | 被 @typescript-eslint/no-loop-func 替代 |
no-magic-numbers | off | 被 @typescript-eslint/no-magic-numbers 替代 |
no-redeclare | off | 被 @typescript-eslint/no-redeclare 替代 |
no-shadow | off | 被 @typescript-eslint/no-shadow 替代 |
space-before-blocks | off | 被 @typescript-eslint/space-before-blocks 替代 |
no-throw-literal | off | 被 @typescript-eslint/no-throw-literal 替代 |
no-unused-expressions | off | 被 @typescript-eslint/no-unused-expressions 替代 |
no-unused-vars | off | 被 @typescript-eslint/no-unused-vars 替代 |
no-use-before-define | off | 被 @typescript-eslint/no-use-before-define 替代 |
no-useless-constructor | off | 被 @typescript-eslint/no-useless-constructor 替代 |
quotes | off | 被 @typescript-eslint/quotes 替代 |
semi | off | 被 @typescript-eslint/semi 替代 |
space-before-function-paren | off | 被 @typescript-eslint/space-before-function-paren 替代 |
require-await | off | 被 @typescript-eslint/require-await 替代 |
no-return-await | off | 被 @typescript-eslint/return-await 替代 |
space-infix-ops | off | 被 @typescript-eslint/space-infix-ops 替代 |
object-curly-spacing | off | 被 @typescript-eslint/object-curly-spacing 替代 |
以下 ESLint 规则,在 *.ts / *.tsx 已被关闭:
| 规则名称 | 错误级别 | 描述 |
|---|---|---|
| constructor-super | off | 禁用构造函数中 super() 调用检查(由 TypeScript 编译器处理) |
| getter-return | off | 禁用 Getter 返回值检查(TS 类型系统已覆盖) |
| no-const-assign | off | 禁用 const 变量重新赋值检查(TS 编译器直接报错) |
| no-dupe-args | off | 禁用函数参数重复检查(TS 类型系统已覆盖) |
| no-dupe-class-members | off | 禁用类成员重复检查(由 @typescript-eslint/no-dupe-class-members 替代) |
| no-dupe-keys | off | 禁用对象属性重复检查(TS 类型系统已覆盖) |
| no-func-assign | off | 禁用函数重新赋值检查(TS 编译器直接报错) |
| no-import-assign | off | 禁用 import 导入值重新赋值检查(TS 类型系统已限制) |
| no-new-symbol | off | 禁用 Symbol 构造函数检查(TS 类型系统已覆盖) |
| no-obj-calls | off | 禁用全局对象函数调用检查(如 Math(),TS 编译器报错) |
| no-redeclare | off | 禁用变量重复声明检查(由 @typescript-eslint/no-redeclare 替代) |
| no-setter-return | off | 禁用 Setter 返回值检查(TS 类型系统已覆盖) |
| no-this-before-super | off | 禁用 super() 前使用 this 检查(TS 编译器直接报错) |
| no-undef | off | 禁用未定义变量检查(TS 类型系统替代此功能) |
| no-unreachable | off | 禁用不可达代码检查(TS 编译器直接报错) |
| no-unsafe-negation | off | 禁用不安全取反操作检查(TS 类型系统已覆盖) |
| valid-typeof | off | 禁用 typeof 校验(TS 类型系统替代此功能) |
| import/named | off | 禁用具名导入校验(TS 类型系统已覆盖) |
| import/no-named-as-default-member | off | 禁用默认导入成员的校验(TS 类型系统处理更精确) |
| import/no-unresolved | off | 禁用模块路径解析校验(由 TS 编译器处理) |
recommended-type-checked
plugin:@typescript-eslint/recommended-type-checked 提供了 TypeScript 推荐的规则,并且这些规则需要类型信息才能工作。它会进行更深层次的分析,例如检查变量类型、函数重载等,从而提供更强大的类型安全保障。
stylistic-type-checked
plugin:@typescript-eslint/strict-type-checked 是一个更严格的 TypeScript 规则集,它在 recommended-type-checked 的基础上增加了更多的规则,以强制执行更严格的类型检查和最佳实践,有助于防止潜在的类型相关错误。
原生 ESLint 规则覆盖说明
以下 ESLint 规则,已被 @typescript-eslint 替代:
| 规则名称 | 错误级别 | 说明 |
|---|---|---|
no-array-constructor | off | 被 @typescript-eslint/no-array-constructor 替代 |
no-implied-eval | off | 被 @typescript-eslint/no-implied-eval 替代 |
no-loss-of-precision | off | 被 @typescript-eslint/no-loss-of-precision 替代 |
no-throw-literal | off | 被 @typescript-eslint/only-throw-error 替代 |
no-unused-vars | off | 被 @typescript-eslint/no-unused-vars 替代 |
no-useless-constructor | off | 被 @typescript-eslint/no-useless-constructor 替代 |
prefer-promise-reject-errors | off | 被 @typescript-eslint/prefer-promise-reject-errors 替代 |
require-await | off | 被 @typescript-eslint/require-await 替代 |