typescript/consistent-type-imports Style
What it does
Enforce consistent usage of type imports by adding or removing the type keyword from imports.
The fixStyle option controls where newly added type keywords are placed when this rule auto-fixes imports. It does not enforce the placement of type keywords that are already present in the code. To enforce consistent placement, use import/consistent-type-specifier-style.
Ignored Files
This rule ignores .astro, .svelte and .vue files entirely. Since Oxlint does not support parsing template syntax, this rule cannot tell if a variable is used or unused in a Vue / Svelte / Astro file.
Why is this bad?
Inconsistent usage of type imports can make the code harder to read and understand.
Examples
Examples of incorrect code for this rule:
import { Foo } from "Foo";
type T = Foo;
type S = import("Foo");Examples of correct code for this rule:
import type { Foo } from "Foo";Examples with "prefer": "type-imports" (default)
Examples of incorrect code:
import { Foo } from "foo";
let foo: Foo;Examples of correct code:
import type { Foo } from "foo";
let foo: Foo;Examples with "prefer": "no-type-imports"
Examples of incorrect code:
import type { Foo } from "foo";
let foo: Foo;Examples of correct code:
import { Foo } from "foo";
let foo: Foo;Examples with "fixStyle": "inline-type-imports"
When fixing type imports, this option will use inline type modifiers:
// Before fixing
import { A, B } from "foo";
type T = A;
const b = B;
// After fixing
import { type A, B } from "foo";
type T = A;
const b = B;Examples with "disallowTypeAnnotations": false
When set to false, allows import() type annotations:
type T = import("foo").Bar;Configuration
This rule accepts a configuration object with the following properties:
disallowTypeAnnotations
type: boolean
default: true
Disallow using import() in type annotations, like type T = import('foo')
fixStyle
type: "separate-type-imports" | "inline-type-imports"
default: "separate-type-imports"
Control how type imports are added when auto-fixing.
"separate-type-imports"
Will add the type keyword after the import keyword import type { A } from '...'
"inline-type-imports"
Will inline the type keyword import { type A } from '...' (only available in TypeScript 4.5+)
prefer
type: "type-imports" | "no-type-imports"
default: "type-imports"
Control whether to enforce type imports or value imports.
"type-imports"
Enforces that you always use import type Foo from '...', except when referenced by decorator metadata.
"no-type-imports"
Will enforce that you always use import Foo from '...'
How to use
To enable this rule using the config file or in the CLI, you can use:
{
"rules": {
"typescript/consistent-type-imports": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/consistent-type-imports": "error",
},
});oxlint --deny typescript/consistent-type-importsVersion
This rule was added in v0.5.2.
