import/no-duplicates Style
What it does
Reports if a resolved path is imported more than once in the same module. This helps avoid unnecessary duplicate imports and keeps the code clean.
Why is this bad?
Importing the same module multiple times can lead to redundancy and unnecessary complexity. It also affects maintainability, as it might confuse developers and result in inconsistent usage of imports across the code.
Examples
Examples of incorrect code for this rule:
import { foo } from "./module";
import { bar } from "./module";
import a from "./module";
import { b } from "./module";Examples of correct code for this rule:
import { foo, bar } from "./module";
import * as a from "foo"; // separate statements for namespace imports
import { b } from "foo";
import { c } from "foo"; // separate type imports, unless
import type { d } from "foo"; // `prefer-inline` is trueConfiguration
This rule accepts a configuration object with the following properties:
considerQueryString
type: boolean
default: false
When set to true, the rule will consider the query string part of the import path when determining if imports are duplicates. This is useful when using loaders like webpack that use query strings to configure how a module should be loaded.
Examples of correct code with this option set to true:
import x from "./bar?optionX";
import y from "./bar?optionY";preferInline
type: boolean
default: false
When set to true, prefer inline type imports instead of separate type import statements for TypeScript code.
Examples of correct code with this option set to true:
import { Foo, type Bar } from "./module";How to use
To enable this rule using the config file or in the CLI, you can use:
{
"plugins": ["import"],
"rules": {
"import/no-duplicates": "error"
}
}oxlint --deny import/no-duplicates --import-plugin