Skip to content
← Back to rules

import/consistent-type-specifier-style 风格

🛠️ An auto-fix is available for this rule for some violations.

它的作用

此规则强制或禁止在命名导入中使用内联的仅类型标记。

为什么这是个问题?

将顶层的 import type { Foo } from 'foo' 与内联的 { type Bar } 混合使用,会迫使读者在浏览导入时不断在上下文之间切换。强制采用一种风格可立即明确区分哪些导入是类型导入,哪些是值导入。

示例

默认 prefer-top-level 选项下的错误代码示例:

typescript
import { type Foo } from "Foo";
import Foo, { type Bar } from "Foo";

默认选项下的正确代码示例:

typescript
import type { Foo } from "Foo";
import type Foo, { Bar } from "Foo";

prefer-inline 选项下的错误代码示例:

typescript
import type { Foo } from "Foo";
import type Foo, { Bar } from "Foo";

prefer-inline 选项下的正确代码示例:

typescript
import { type Foo } from "Foo";
import Foo, { type Bar } from "Foo";

配置

此规则接受以下字符串值之一:

"prefer-top-level"

对类型导入优先使用 import type { Foo } from 'foo'

"prefer-inline"

对类型导入优先使用 import { type Foo } from 'foo'

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "plugins": ["import"],
  "rules": {
    "import/consistent-type-specifier-style": "error"
  }
}
bash
oxlint --deny import/consistent-type-specifier-style --import-plugin

参考资料