useGroupedTypeImport (since v1.0.0)
Diagnostic Category: lint/nursery/useGroupedTypeImport
Source: no-import-type-side-effects
Enforce the use of import type
when an import
only has specifiers with type
qualifier.
The --verbatimModuleSyntax
TypeScript’s compiler option causes TypeScript to do simple and predictable transpilation on import
declarations.
Namely, it completely removes import type
and any imported names with the type
qualifier.
For instance, the following code:
import { type A, type B } from "mod-1";import type { C, D } from "mod-2";
nursery/useGroupedTypeImport.js:1:8 lint/nursery/useGroupedTypeImport FIXABLE ━━━━━━━━━━━━━━━━━━━━
✖ The type qualifier can be moved just after import to completely remove the import at compile time.
> 1 │ import { type A, type B } from "mod-1";
│ ^^^^^^^^^^^^^^^^^^
2 │ import type { C, D } from "mod-2";
3 │
ℹ Only import type are removed at compile time.
ℹ Unsafe fix: Use import type instead.
1 │ - import·{·type·A,·type·B·}·from·"mod-1";
1 │ + import·type·{·A,·B·}·from·"mod-1";
2 2 │ import type { C, D } from "mod-2";
3 3 │
is transpiled to:
import "mod-1";
Note that, an import
that includes only names qualified with type
is transpiled to a side-effect import
.
This can be a surprising behavior: most of developers could expect the deletion of the import
.
This behavior may still be desirable for applying the potential side-effects of the imported module.
In most cases you will not want to leave behind an unnecessary side effect import
.
In teh remaining cases, it is often preferable to explicitly use a side-effect import
to apply the side-effects of a module:
import "mod"; // side-effect importimport type { A, B } from "mod";
Examples
Section titled ExamplesInvalid
Section titled Invalidimport { type A } from "mod";
nursery/useGroupedTypeImport.js:1:8 lint/nursery/useGroupedTypeImport FIXABLE ━━━━━━━━━━━━━━━━━━━━
✖ The type qualifier can be moved just after import to completely remove the import at compile time.
> 1 │ import { type A } from "mod";
│ ^^^^^^^^^^
2 │
ℹ Only import type are removed at compile time.
ℹ Unsafe fix: Use import type instead.
1 │ - import·{·type·A·}·from·"mod";
1 │ + import·type·{·A·}·from·"mod";
2 2 │
Valid
Section titled Validimport type { A, B } from "mod";
import { A, type B } from "mod";