Skip to main content

no-import-type-side-effects

Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers.

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复


--verbatimModuleSyntax 编译器选项使 TypeScript 对导入声明进行简单且可预测的转译。 也就是说,它完全删除了带有顶层 type 限定符的导入声明,并且删除了带有内联 type 限定符的所有导入说明符。

英:The --verbatimModuleSyntax compiler option causes TypeScript to do simple and predictable transpilation on import declarations. Namely, it completely removes import declarations with a top-level type qualifier, and it removes any import specifiers with an inline type qualifier.

后一种行为确实会产生一个潜在的令人惊讶的影响,因为在某些情况下 TS 可能会在运行时留下 "副作用" 导入:

英:The latter behavior does have one potentially surprising effect in that in certain cases TS can leave behind a "side effect" import at runtime:

import { type A, type B } from 'mod';

// is transpiled to

import {} from 'mod';
// which is the same as
import 'mod';

对于需要导入副作用的罕见情况,这可能是可取的 - 但在大多数情况下,你不希望留下不必要的副作用导入。

英:For the rare case of needing to import for side effects, this may be desirable - but for most cases you will not want to leave behind an unnecessary side effect import.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-import-type-side-effects": "error"
}
};
在线运行试试这个规则 ↗

示例

此规则强制你在仅导入具有内联 type 限定符的说明符时使用顶层 type 限定符进行导入

英:This rule enforces that you use a top-level type qualifier for imports when it only imports specifiers with an inline type qualifier

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';
Open in Playground

何时不使用它

如果你不使用 TypeScript 5.0 的 verbatimModuleSyntax 选项,并且你的项目是使用为你管理导入副作用的打包程序构建的,则此规则可能对你没有那么有用。

英:If you're not using TypeScript 5.0's verbatimModuleSyntax option and your project is built with a bundler that manages import side effects for you, this rule may not be as useful for you.

选项

该规则不可配置。

资源