インポート型副作用の禁止
インポートにインライン型修飾子のみを持つ指定子がある場合、トップレベルのインポート型修飾子の使用を強制します。
🔧
このルールによって報告されるいくつかの問題は、 --fix
ESLintコマンドラインオプションによって自動的に修正可能です。.
--verbatimModuleSyntax
コンパイラオプションを使用すると、TypeScriptはインポート宣言に対してシンプルで予測可能なトランスパイルを実行します。つまり、トップレベルのtype
修飾子を持つインポート宣言を完全に削除し、インラインtype
修飾子を持つインポート指定子を削除します。
後者の動作には、特定のケースでTSが実行時に「副作用」インポートを残す可能性があるという、潜在的に驚くべき効果が1つあります。
import { type A, type B } from 'mod';
// is transpiled to
import {} from 'mod';
// which is the same as
import 'mod';
副作用のためにインポートする必要があるまれなケースでは、これは望ましい場合がありますが、ほとんどのケースでは、不要な副作用インポートを残したくないでしょう。
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-import-type-side-effects": "error"
}
};
プレイグラウンドでこのルールを試す ↗
例
このルールは、インラインtype
修飾子を持つ指定子のみをインポートする場合、インポートにトップレベルのtype
修飾子を使用することを強制します。
- ❌ 間違い
- ✅ 正解
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';
プレイグラウンドで開く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';
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';
プレイグラウンドで開くオプション
このルールは設定できません。
使用しない場合
TypeScript 5.0のverbatimModuleSyntax
オプションを使用しておらず、インポートの副作用を管理するバンドラーでプロジェクトがビルドされている場合、このルールは役に立たない可能性があります。