useImportType (since v1.5.0)
Diagnostic Category: lint/nursery/useImportType
Inspired from: 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
.
Examples
Section titled ExamplesInvalid
Section titled Invalidimport { A } from "./mod.js";type TypeOfA = typeof A;let a: A;
nursery/useImportType.js:1:1 lint/nursery/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";
│ +++++
import { type A, type B } from "./mod.js";
nursery/useImportType.js:1:1 lint/nursery/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 │
Valid
Section titled Validimport type { A } from "./mod.js";let a: A;
import { B } from "./mod.js";let a: B = new B();
The rule ignores unused imports and imports with import attributes.
import { A } from "./mod.js";
import { B } from "./mod.js" with {};export type { B };