no-restricted-imports
import
でロードされる場合、指定されたモジュールを許可しません。
このルールは、基本の eslint/no-restricted-imports
ルールを拡張します。型インポート (import type X from "..."
, import { type X } from "..."
) と import x = require("...")
構文のサポートが追加されています。
使用方法
.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"no-restricted-imports": "off",
"@typescript-eslint/no-restricted-imports": "error"
}
};
プレイグラウンドでこのルールを試す ↗
オプション
eslint/no-restricted-imports
オプションを参照してください。
このルールは、以下のオプションを追加します
allowTypeImports
(デフォルト: false
)
特定のパスまたはパターンに対して、このオプションを次のように指定できます
{
"rules": {
"@typescript-eslint/no-restricted-imports": [
"error",
{
"paths": [
{
"name": "import-foo",
"message": "Please use import-bar instead.",
"allowTypeImports": true,
},
{
"name": "import-baz",
"message": "Please use import-quux instead.",
"allowTypeImports": true,
},
],
},
],
},
}
true
に設定すると、ルールは 型のみのインポートを許可します。
上記の構成を使用したコードの例
- ❌ 間違い
- ✅ 正解
import foo from 'import-foo';
export { Foo } from 'import-foo';
import baz from 'import-baz';
export { Baz } from 'import-baz';
プレイグラウンドで開くimport { foo } from 'other-module';
import type foo from 'import-foo';
export type { Foo } from 'import-foo';
import type baz from 'import-baz';
export type { Baz } from 'import-baz';
プレイグラウンドで開く