no-import-type-side-effects
僅當匯入僅具有內嵌類型限定詞的指定項時,強制使用頂層匯入類型限定詞。
🔧
此規則報告的一些問題可由 --fix
ESLint 命令列選項自動修復.
--verbatimModuleSyntax
編譯器選項使 TypeScript 對匯入宣告執行簡單且可預測的轉譯。也就是說,它會完全刪除具有頂層 type
限定詞的匯入宣告,並移除任何具有內嵌 type
限定詞的匯入指定項。
後一種行為確實有一個潛在令人驚訝的效果,因為在某些情況下,TS 可以於執行時留下「副作用」匯入
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
選項,而您的專案是使用為您管理導入程式副作用的打包程式建置的,此規則對您來說可能沒那麼有用。
相關
consistent-type-imports
import/consistent-type-specifier-style
import/no-duplicates
與{"prefer-inline": true}