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.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-import-type-side-effects": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

此规则强制在仅导入带有内联 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

选项

该规则不可配置。

何时不使用它

¥When Not To Use It

如果你不使用 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.

相关

¥Related To

'## 资源'