no-duplicate-imports

禁止重复模块导入

每个模块使用单个 import 语句将使代码更清晰,因为你可以在一行中看到从该模块导入的所有内容。

¥Using a single import statement per module will make the code clearer because you can see everything being imported from that module on one line.

在以下示例中,第 1 行的 module 导入在第 3 行重复。这些可以结合起来使导入清单更加简洁。

¥In the following example the module import on line 1 is repeated on line 3. These can be combined to make the list of imports more succinct.

import { merge } from 'module';
import something from 'another-module';
import { find } from 'module';

规则详情

¥Rule Details

此规则要求来自单个模块的所有可以合并的导入都存在于单个 import 语句中。

¥This rule requires that all imports from a single module that can be merged exist in a single import statement.

此规则的错误代码示例:

¥Example of incorrect code for this rule:

在线运行
/*eslint no-duplicate-imports: "error"*/

import { merge } from 'module';
import something from 'another-module';
import { find } from 'module';

此规则的正确代码示例:

¥Example of correct code for this rule:

在线运行
/*eslint no-duplicate-imports: "error"*/

import { merge, find } from 'module';
import something from 'another-module';

此规则的正确代码示例:

¥Example of correct code for this rule:

在线运行
/*eslint no-duplicate-imports: "error"*/

// not mergeable
import { merge } from 'module';
import * as something from 'module';

选项

¥Options

此规则有一个对象选项:

¥This rule has an object option:

  • "includeExports"true(默认 false)除了检查导入外,还检查导出。

    ¥"includeExports": true (default false) checks for exports in addition to imports.

  • "allowSeparateTypeImports"true(默认 false)允许在 TypeScript 文件中从同一模块导入类型和值。

    ¥"allowSeparateTypeImports": true (default false) allows a type import alongside a value import from the same module in TypeScript files.

includeExports

如果从导入的模块重新导出,你应该将导入添加到 import 语句中,然后直接导出,而不是使用 export ... from

¥If re-exporting from an imported module, you should add the imports to the import-statement, and export that directly, not use export ... from.

使用 { "includeExports": true } 选项的此规则的错误代码示例:

¥Example of incorrect code for this rule with the { "includeExports": true } option:

在线运行
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge } from 'module';

export { find } from 'module';

使用 { "includeExports": true } 选项的此规则的正确代码示例:

¥Example of correct code for this rule with the { "includeExports": true } option:

在线运行
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge, find } from 'module';

export { find };

使用 { "includeExports": true } 选项的此规则的正确代码示例:

¥Example of correct code for this rule with the { "includeExports": true } option:

在线运行
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge, find } from 'module';

// cannot be merged with the above import
export * as something from 'module';

// cannot be written differently
export * from 'module';

allowSeparateTypeImports

TypeScript 允许使用 import type 导入类型。默认情况下,此规则会标记与 import 具有相同说明符的 import type 实例。allowSeparateTypeImports 选项允许你覆盖此行为。

¥TypeScript allows importing types using import type. By default, this rule flags instances of import type that have the same specifier as import. The allowSeparateTypeImports option allows you to override this behavior.

此规则使用默认 { "allowSeparateTypeImports": false } 选项的错误 TypeScript 代码示例:

¥Example of incorrect TypeScript code for this rule with the default { "allowSeparateTypeImports": false } option:

/*eslint no-duplicate-imports: ["error", { "allowSeparateTypeImports": false }]*/

import { someValue } from 'module';
import type { SomeType } from 'module';

此规则使用默认 { "allowSeparateTypeImports": false } 选项的正确 TypeScript 代码示例:

¥Example of correct TypeScript code for this rule with the default { "allowSeparateTypeImports": false } option:

/*eslint no-duplicate-imports: ["error", { "allowSeparateTypeImports": false }]*/

import { someValue, type SomeType } from 'module';

此规则使用 { "allowSeparateTypeImports": true } 选项的错误 TypeScript 代码示例:

¥Example of incorrect TypeScript code for this rule with the { "allowSeparateTypeImports": true } option:

/*eslint no-duplicate-imports: ["error", { "allowSeparateTypeImports": true }]*/

import { someValue } from 'module';
import type { SomeType } from 'module';
import type { AnotherType } from 'module';

此规则使用 { "allowSeparateTypeImports": true } 选项的正确 TypeScript 代码示例:

¥Example of correct TypeScript code for this rule with the { "allowSeparateTypeImports": true } option:

/*eslint no-duplicate-imports: ["error", { "allowSeparateTypeImports": true }]*/

import { someValue } from 'module';
import type { SomeType, AnotherType } from 'module';

版本

此规则是在 ESLint v2.5.0 中引入。

资源