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.
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 Playgroundimport 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';
import T from 'mod';
import type T from 'mod';
import * as T from 'mod';
import type * as T from 'mod';
import { T } from 'mod';
import type { T } from 'mod';
import { T, U } from 'mod';
import type { T, U } from 'mod';
import { type T, U } from 'mod';
import { T, type U } from 'mod';
import type T, { U } from 'mod';
import T, { type U } 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.
相关
consistent-type-imports
import/consistent-type-specifier-style
import/no-duplicates
与{"prefer-inline": true}
选项
该规则不可配置。