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"。规则添加和删除被视为重大更改,并且仅在主要版本更新中完成。

¥With the exception of all, strict, and strict-type-checked, all configurations are considered "stable". Rule additions and removals are treated as breaking changes and will only be done in major version bumps.

入门

¥Getting Started

首先查看 入门 > 快速入门 以设置你的 ESLint 配置文件。软件包 > typescript-eslint 包含有关 tseslint 助手的更多文档。

¥See Getting Started > Quickstart first to set up your ESLint configuration file. Packages > typescript-eslint includes more documentation on the tseslint helper.

eslint.config.mjs
// @ts-check

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommended,
);

没有类型检查的项目

¥Projects Without Type Checking

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

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

eslint.config.mjs
export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommended,
tseslint.configs.stylistic,
);

如果在你的项目中工作的大多数开发者都熟悉 TypeScript 和 typescript-eslint,请考虑用 strict 替换 recommended

¥If a majority of developers working on your project are comfortable with TypeScript and typescript-eslint, consider replacing recommended with strict.

使用类型检查的项目

¥Projects With Type Checking

如果你的项目启用了 类型化的 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:

eslint.config.mjs
export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
tseslint.configs.stylisticTypeChecked,
);

如果在你的项目中工作的大多数开发者都熟悉 TypeScript 和 typescript-eslint,请考虑用 strict-type-checked 替换 recommended-type-checked

¥If a majority of developers working on your project are comfortable with TypeScript and typescript-eslint, consider replacing recommended-type-checked with strict-type-checked.

推荐配置

¥Recommended Configurations

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

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

  • recommended:代码正确性的推荐规则,你可以直接使用,无需额外配置。

    ¥recommended: Recommended rules for code correctness that you can drop in without additional configuration.

  • recommended-type-checked:包含 recommended + 需要类型信息的其他推荐规则。

    ¥recommended-type-checked: Contains recommended + additional recommended rules that require type information.

  • strict:包含 recommended + 也可以捕获错误但比推荐规则更具主观性的严格规则。

    ¥strict: Contains recommended + additional strict rules that can also catch bugs but are more opinionated than recommended rules.

  • strict-type-checked:包含 strict + 需要类型信息的其他严格规则。

    ¥strict-type-checked: Contains strict + additional strict rules require type information.

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

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

  • stylistic:你可以直接使用风格规则,无需额外配置。

    ¥stylistic: Stylistic rules you can drop in without additional configuration.

  • stylistic-type-checked:包含 stylistic + 需要类型信息的其他风格规则。

    ¥stylistic-type-checked: Contains stylistic + additional stylistic rules that require type information.

注意

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

¥These configurations are our recommended starting points, but you don't need to use them as-is. ESLint allows configuring own rule settings on top of extended configurations. See ESLint's Configuring Rules docs.

代码正确性的推荐规则,你可以直接使用,无需额外配置。这些规则的报告几乎总是针对不良实践和/或可能的错误。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.

eslint.config.mjs
export default tseslint.config(
tseslint.configs.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.

eslint.config.mjs
export default tseslint.config(
tseslint.configs.recommendedTypeChecked,
);

有关此配置的确切内容,请参阅 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.

eslint.config.mjs
export default tseslint.config(
tseslint.configs.strict,
);

一些在 recommended 中启用的规则默认在此配置中使用更严格的设置。有关此配置的确切内容,请参阅 configs/strict.ts

¥Some rules also enabled in recommended default to more strict settings in this configuration. See configs/strict.ts for the exact contents of this config.

提示

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

¥We recommend a TypeScript project extend from plugin:@typescript-eslint/strict only if a nontrivial percentage of its developers are highly proficient in TypeScript.

警告

此配置在语义版本控制 (semver) 下不被视为 "stable"。其启用的规则和/或其选项可能会在主要版本更新之外发生变化。

¥This configuration is not considered "stable" under Semantic Versioning (semver). Its enabled rules and/or their options may change outside of major version updates.

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.

eslint.config.mjs
export default tseslint.config(
tseslint.configs.strictTypeChecked,
);

一些在 recommended-type-checked 中启用的规则默认在此配置中使用更严格的设置。有关此配置的确切内容,请参阅 configs/strict-type-checked.ts

¥Some rules also enabled in recommended-type-checked default to more strict settings in this configuration. See configs/strict-type-checked.ts for the exact contents of this config.

提示

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

¥We recommend a TypeScript project extend from plugin:@typescript-eslint/strict-type-checked only if a nontrivial percentage of its developers are highly proficient in TypeScript.

警告

此配置在语义版本控制 (semver) 下不被视为 "stable"。其启用的规则和/或其选项可能会在主要版本更新之外发生变化。

¥This configuration is not considered "stable" under Semantic Versioning (semver). Its enabled rules and/or their options may change outside of major version updates.

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.

eslint.config.mjs
export default tseslint.config(
tseslint.configs.stylistic,
);

请注意,stylistic 不会替换 recommendedstrictstylistic 增加了额外的规则。

¥Note that stylistic does not replace recommended or strict. stylistic adds additional rules.

有关此配置的确切内容,请参阅 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.

eslint.config.mjs
export default tseslint.config(
tseslint.configs.stylisticTypeChecked,
);

请注意,stylistic-type-checked 不会替换 recommended-type-checkedstrict-type-checkedstylistic-type-checked 增加了额外的规则。

¥Note that stylistic-type-checked does not replace recommended-type-checked or strict-type-checked. stylistic-type-checked adds additional rules.

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

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

其他配置

¥Other Configurations

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.

警告

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

¥We do not recommend TypeScript projects extend from plugin:@typescript-eslint/all. Many rules conflict with each other and/or are intended to be configured per-project.

警告

此配置在语义版本控制 (semver) 下不被视为 "stable"。其启用的规则和/或其选项可能会在主要版本更新之外发生变化。

¥This configuration is not considered "stable" under Semantic Versioning (semver). Its enabled rules and/or their options may change outside of major version updates.

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.

信息

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

¥If you use type-aware rules from other plugins, you will need to manually disable these rules or use a premade config they provide to disable them.

eslint.config.mjs
export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
files: ['**/*.js'],
extends: [tseslint.configs.disableTypeChecked],
},
);
警告

此配置在语义版本控制 (semver) 下不被视为 "stable"。其启用的规则和/或其选项可能会在主要版本更新之外发生变化。

¥This configuration is not considered "stable" under Semantic Versioning (semver). Its enabled rules and/or their options may change outside of major version updates.

此规则集旨在在扩展 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.

eslint.config.mjs
export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.eslintRecommended,
);

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

¥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.

recommended 的一个版本,仅包含类型检查规则并禁用任何相应的核心 ESLint 规则。此配置加上 recommended 相当于 recommended-type-checked

¥A version of recommended that only contains type-checked rules and disables of any corresponding core ESLint rules. This config plus recommended is equivalent to recommended-type-checked.

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

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

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

strict-type-checked-only

strict 的一个版本,仅包含类型检查规则并禁用任何相应的核心 ESLint 规则。此配置加上 strict 相当于 strict-type-checked

¥A version of strict that only contains type-checked rules and disables of any corresponding core ESLint rules. This config plus strict is equivalent to strict-type-checked.

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

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

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

警告

此配置在语义版本控制 (semver) 下不被视为 "stable"。其启用的规则和/或其选项可能会在主要版本更新之外发生变化。

¥This configuration is not considered "stable" under Semantic Versioning (semver). Its enabled rules and/or their options may change outside of major version updates.

stylistic-type-checked-only

stylistic 的一个版本,仅包含类型检查规则并禁用任何相应的核心 ESLint 规则。此配置加上 stylistic 相当于 stylistic-type-checked

¥A version of stylistic that only contains type-checked rules and disables of any corresponding core ESLint rules. This config plus stylistic is equivalent to stylistic-type-checked.

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

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

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

建议配置更改

¥Suggesting Configuration Changes

如果你强烈认为特定规则应该(或不应该)是这些配置之一,请 提出问题 以及解释你的理由的详细参数。

¥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.

格式化

¥Formatting

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.