useImportType (since v1.5.0)
Diagnostic Category: lint/style/useImportType
Sources:
- Inspired from:
@typescript-eslint/consistent-type-imports
Promotes the use of import type
for types.
TypeScript allows specifying a type
qualifier on an import
to indicate that the import
doesn’t exist at runtime.
This allows transpilers to safely drop imports of types without looking for their definition.
This also ensures that some modules are not loaded at runtime.
The rule ensures that all imports used only as a type use a type-only import
.
It also groups inline type imports into a grouped import type
.
Caveat with TypeScript experimental decorators
Section titled Caveat with TypeScript experimental decoratorsSome frameworks like Angular and NestJS rely on experimental TypeScript decorators which allow code to be generated based on type annotations. This is mainly used for dependency injection.
Since Biome doesn’t know how a decorator is implemented, it is unable to detect that an import used as a type is also used as a value in the code generated by a decorator. This leads Biome to suggest importing some imports as type, which are actually used as value at runtime.
We haven’t found a way to support this pattern yet. We recommend disabling this rule when using such decorators.
Options
Section titled OptionsThis rule respects the jsxRuntime
setting and will make an exception for React globals if it is set to
"reactClassic"
.
Examples
Section titled ExamplesInvalid
Section titled Invalidstyle/useImportType.js:1:1 lint/style/useImportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ All these imports are only used as types.
> 1 │ import { A } from "./mod.js";
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ type TypeOfA = typeof A;
3 │ let a: A;
ℹ Importing the types with import type ensures that they are removed by the transpilers and avoids loading unnecessary modules.
ℹ Safe fix: Use import type.
1 │ import·type·{·A·}·from·"./mod.js";
│ +++++
style/useImportType.js:1:1 lint/style/useImportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ All these imports are only used as types.
> 1 │ import { type A, type B } from "./mod.js";
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Importing the types with import type ensures that they are removed by the transpilers and avoids loading unnecessary modules.
ℹ Safe fix: Use import type.
1 │ - import·{·type·A,·type·B·}·from·"./mod.js";
1 │ + import·type·{·A,·B·}·from·"./mod.js";
2 2 │
style/useImportType.js:1:1 lint/style/useImportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ All these imports are only used as types.
> 1 │ import { type A, B } from "./mod.js";
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ let c: A;
3 │ let d: typeof B;
ℹ Importing the types with import type ensures that they are removed by the transpilers and avoids loading unnecessary modules.
ℹ Safe fix: Use import type.
1 │ - import·{·type·A,·B·}·from·"./mod.js";
1 │ + import·type·{·A,·B·}·from·"./mod.js";
2 2 │ let c: A;
3 3 │ let d: typeof B;
Valid
Section titled ValidThe rule ignores unused imports and imports with import attributes.