Skip to main content

配置


ESLint 可共享配置 的存在是为了提供你可以开始使用的规则设置的完整列表。 @typescript-eslint/eslint-plugin 包含内置配置,你可以从中扩展以引入推荐的起始规则。

英:ESLint shareable configurations exist to provide a comprehensive list of rules settings that you can start with. @typescript-eslint/eslint-plugin includes built-in configurations you can extend from to pull in the recommended starting rules.

allstrictstrict-type-checked 之外,所有配置均被视为 "stable"。 规则添加和删除被视为重大更改,并且仅在主要版本更新中完成。

入门

没有类型检查的项目

如果你的项目没有启用 类型化的 linting,我们建议启用 recommendedstylistic 配置来启动:

英:If your project does not enable typed linting, we suggest enabling the recommended and stylistic configurations to start:

.eslintrc.js
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/stylistic',
],
};

如果从事你项目的大多数开发者都熟悉 TypeScript 和 typescript-eslint,请考虑将 recommended 替换为 strict

使用类型检查的项目

如果你的项目启用了 类型化的 linting,我们建议启用 recommended-type-checkedstylistic-type-checked 配置来启动:

英:If your project enables typed linting, we suggest enabling the recommended-type-checked and stylistic-type-checked configurations to start:

.eslintrc.js
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
],
};

如果从事你项目的大多数开发者都熟悉 TypeScript 和 typescript-eslint,请考虑将 recommended-type-checked 替换为 strict-type-checked

我们建议大多数项目应从以下之一扩展:

英:We recommend that most projects should extend from one of:

  • recommended: 代码正确性的推荐规则,你可以直接使用,无需额外配置。
  • recommended-type-checked: 包含 recommended + 需要类型信息的附加推荐规则。
  • strict: 包含 recommended + 额外的严格规则,也可以捕获错误,但比推荐的规则更加有态度。
  • strict-type-checked: 包含 strict + 附加严格规则需要类型信息。

此外,我们提供了 stylistic 配置,强制执行简洁且一致的代码。 我们建议大多数项目应从以下任一扩展:

英:Additionally, we provide a stylistic config that enforces concise and consistent code. We recommend that most projects should extend from either:

  • stylistic: 你可以直接使用风格规则,无需额外配置。
  • stylistic-type-checked: 包含 stylistic + 需要类型信息的附加风格规则。
注意

这些配置是我们推荐的起点,但是 你不需要按原样使用它们。 ESLint 允许在扩展配置之上配置自己的规则设置。 参见 ESLint 的配置规则文档

代码正确性的推荐规则,你可以直接使用,无需额外配置。 这些规则的报告几乎总是针对不良实践和/或可能的错误。 recommended 还禁用已知与 typescript-eslint 规则冲突或导致 TypeScript 代码库出现问题的核心 ESLint 规则。

英:Recommended rules for code correctness that you can drop in without additional configuration. These rules are those whose reports are almost always for a bad practice and/or likely bug. recommended also disables core ESLint rules known to conflict with typescript-eslint rules or cause issues in TypeScript codebases.

.eslintrc.js
module.exports = {
extends: ['plugin:@typescript-eslint/recommended'],
};

有关此配置的具体内容,请参阅 configs/recommended.ts

英:See configs/recommended.ts for the exact contents of this config.

包含所有 recommended 以及需要类型信息的其他推荐规则。 此配置中新添加的规则与 recommended 中的规则类似有用。

英:Contains all of recommended along with additional recommended rules that require type information. Rules newly added in this configuration are similarly useful to those in recommended.

.eslintrc.js
module.exports = {
extends: ['plugin:@typescript-eslint/recommended-type-checked'],
};

有关此配置的具体内容,请参阅 configs/recommended-type-checked.ts

英:See configs/recommended-type-checked.ts for the exact contents of this config.

strict

包含 recommended 的全部内容,以及还可以捕获错误的附加严格规则。 strict 中添加的规则比推荐的规则更加有态度,并且可能不适用于所有项目。

英:Contains all of recommended, as well as additional strict rules that can also catch bugs. Rules added in strict are more opinionated than recommended rules and might not apply to all projects.

.eslintrc.js
module.exports = {
extends: ['plugin:@typescript-eslint/strict'],
};

有关此配置的具体内容,请参阅 configs/strict.ts

英:See configs/strict.ts for the exact contents of this config.

提醒

仅当 TypeScript 项目中有相当一部分开发者高度精通 TypeScript 时,我们才建议从 plugin:@typescript-eslint/strict 开始扩展 TypeScript 项目。

strict-type-checked

包含所有 recommendedrecommended-type-checkedstrict,以及需要类型信息的其他严格规则。 此配置中新添加的规则与 strict 中的规则类似有用(且有态度)。

英:Contains all of recommended, recommended-type-checked, and strict, along with additional strict rules that require type information. Rules newly added in this configuration are similarly useful (and opinionated) to those in strict.

.eslintrc.js
module.exports = {
extends: ['plugin:@typescript-eslint/strict-type-checked'],
};

有关此配置的具体内容,请参阅 configs/strict-type-checked.ts

英:See configs/strict-type-checked.ts for the exact contents of this config.

提醒

仅当 TypeScript 项目中有相当一部分开发者高度精通 TypeScript 时,我们才建议从 plugin:@typescript-eslint/strict-type-checked 开始扩展 TypeScript 项目。

stylistic

规则被认为是现代 TypeScript 代码库的最佳实践,但这不会影响程序逻辑。 这些规则通常是关于强制执行更简单的代码模式。

英:Rules considered to be best practice for modern TypeScript codebases, but that do not impact program logic. These rules are generally opinionated about enforcing simpler code patterns.

.eslintrc.js
module.exports = {
extends: ['plugin:@typescript-eslint/stylistic'],
};

有关此配置的具体内容,请参阅 configs/stylistic.ts

英:See configs/stylistic.ts for the exact contents of this config.

stylistic-type-checked

包含 stylistic 的全部内容,以及需要类型信息的其他风格规则。 此配置中新添加的规则与 stylistic 中的规则类似。

英:Contains all of stylistic, along with additional stylistic rules that require type information. Rules newly added in this configuration are similarly opinionated to those in stylistic.

.eslintrc.js
module.exports = {
extends: ['plugin:@typescript-eslint/stylistic-type-checked'],
};

有关此配置的具体内容,请参阅 configs/stylistic-type-checked.ts

英:See configs/stylistic-type-checked.ts for the exact contents of this config.

其他配置

typescript-eslint 包含一些实用程序配置。

英:typescript-eslint includes a few utility configurations.

all

启用作为 typescript-eslint 一部分提供的每个规则。 请注意,许多规则并不适用于所有代码库,或者需要进行配置。

英:Enables each the rules provided as a part of typescript-eslint. Note that many rules are not applicable in all codebases, or are meant to be configured.

有关此配置的具体内容,请参阅 configs/all.ts

英:See configs/all.ts for the exact contents of this config.

警告

我们不建议从 plugin:@typescript-eslint/all 扩展 TypeScript 项目。 许多规则相互冲突和/或旨在针对每个项目进行配置。

base

一个最小的规则集,仅设置运行 typescript-eslint 所需的解析器和插件选项。 我们不建议直接使用它; 相反,应扩展先前推荐的规则。

英:A minimal ruleset that sets only the required parser and plugin options needed to run typescript-eslint. We don't recommend using this directly; instead, extend from an earlier recommended rule.

如果你使用任何推荐的配置,则会自动包含此配置。

英:This config is automatically included if you use any of the recommended configurations.

有关此配置的具体内容,请参阅 configs/base.ts

英:See configs/base.ts for the exact contents of this config.

disable-type-checked

一个实用程序规则集,将禁用我们项目中可用的类型感知 linting 和所有类型感知规则。 如果你希望基本配置与类型感知 linting 相关,然后有条件地使用 overrides 在代码库的特定子集上禁用类型感知 linting,则此配置非常有用。

英:A utility ruleset that will disable type-aware linting and all type-aware rules available in our project. This config is useful if you'd like to have your base config concerned with type-aware linting, and then conditionally use overrides to disable type-aware linting on specific subsets of your codebase.

有关此配置的具体内容,请参阅 configs/disable-type-checked.ts

英:See configs/disable-type-checked.ts for the exact contents of this config.

信息

如果你使用其他插件的类型感知规则,则需要手动禁用这些规则或使用它们提供的预制配置来禁用它们。

.eslintrc.js
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
],
overrides: [
{
files: ['*.js'],
extends: ['plugin:@typescript-eslint/disable-type-checked'],
},
],
};

该规则集旨在扩展 eslint:recommended 后使用。 它禁用已由 TypeScript 编译器检查的核心 ESLint 规则。 此外,它还支持促进使用 TypeScript 允许的更现代结构的规则。

英:This ruleset is meant to be used after extending eslint:recommended. It disables core ESLint rules that are already checked by the TypeScript compiler. Additionally, it enables rules that promote using the more modern constructs TypeScript allows for.

.eslintrc.js
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
],
};

如果你使用任何推荐的配置,则会自动包含此配置。

英:This config is automatically included if you use any of the recommended configurations.

有关此配置的具体内容,请参阅 configs/eslint-recommended.ts

英:See configs/eslint-recommended.ts for the exact contents of this config.

建议配置更改

如果你强烈认为某个特定规则应该(或不应该)成为这些配置之一,请 提出问题 以及 detailed 参数解释你的推断。

英:If you feel strongly that a specific rule should (or should not) be one of these configurations, please file an issue along with a detailed argument explaining your reasoning.

格式化

typescript-eslint 提供的预设配置均未启用格式化规则(仅用于强制执行代码空格和其他琐事的规则)。 我们强烈建议你使用 Prettier 或等效工具来格式化代码,而不是 ESLint 格式化规则。 参见 格式化怎么样? > 建议使用方法

英:None of the preset configs provided by typescript-eslint enable formatting rules (rules that only serve to enforce code whitespace and other trivia). We strongly recommend you use Prettier or an equivalent for formatting your code, not ESLint formatting rules. See What About Formatting? > Suggested Usage.