import/consistent-type-specifier-style
Enforce one way to mark type-only imports: import type { A } or import { type A }
Also called import-x/consistent-type-specifier-style
TypeScript has two ways to mark an import as type-only: import type { User } for the whole statement, or import { type User } for one name. This rule keeps a project on one of them.
The two compile differently with verbatimModuleSyntax. import type { User } from "./user" is removed, while import { type User } from "./user" is left as import {} from "./user" and still loads the module.
By default the rule asks for import type. Imports without braces, such as import type User from "./user", are never reported.
--fix rewrites the import in the style you chose.
Configure#
Off by default. To turn it on:
[lint.rules]
"import/consistent-type-specifier-style" = "error" # or "warn"Every option, set to its default:
[lint.rules]
# "prefer-inline" or "prefer-top-level"
"import/consistent-type-specifier-style" = ["error", "prefer-top-level"]Examples#
import { type User, type Session } from "./models";Prefer using a top-level type-only import instead of inline type specifiers.import { createUser, type UserPrefer using a top-level type-only import instead of inline type specifiers. } from "./models";import type { User, Session } from "./models";import { createUser } from "./models";
import type { User } from "./models";Options#
[lint.rules]
"import/consistent-type-specifier-style" = ["error", "prefer-inline"]| Option | What it does |
|---|---|
"prefer-top-level" (default) | Report import { type User }. Types go in their own import type statement. |
"prefer-inline" | Report import type { User }. Each type is marked by name, so one import statement per module is enough. |
"prefer-inline"1 problemimport { createUser } from "./models";
import type { User } from "./models";Prefer using inline type specifiers instead of a top-level type-only import."prefer-inline"No problemsimport { createUser, type User } from "./models";