typescript/no-import-type-side-effects 限制
它的作用
当一个 import 仅包含带有内联类型限定符的导入项时,强制使用顶层的 import type 限定符。
为什么这不好?
--verbatimModuleSyntax 编译器选项会让 TypeScript 对 import 声明进行简单且可预测的转译。具体来说,它会完全移除带有顶层类型限定符的 import 声明,并移除任何带有内联类型限定符的 import 导入项。
后一种行为在某些情况下确实会带来一个可能令人意外的影响:TS 可能会在运行时保留一个“副作用” import:
ts
import { type A, type B } from "mod";会被转译为
ts
import {} from "mod";
// 这与以下内容相同
import "mod";对于极少数需要仅因副作用而导入的情况,这样做可能是可取的——但在大多数情况下,你不会希望留下一个不必要的副作用 import。
示例
此规则的错误代码示例:
ts
import { type A } from "mod";
import { type A as AA } from "mod";
import { type A, type B } from "mod";
import { type A as AA, type B as BB } from "mod";此规则的正确代码示例:
ts
import type { A } from "mod";
import type { A as AA } from "mod";
import type { A, B } from "mod";
import type { A as AA, B as BB } from "mod";如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/no-import-type-side-effects": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/no-import-type-side-effects": "error",
},
});bash
oxlint --deny typescript/no-import-type-side-effects版本
此规则自 v0.5.0 起新增。
