跳到主要內容

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';
在遊樂場中開啟

資源

以 ❤️ 取自 ESLint 核心