no-restricted-imports
Disallow specified modules when loaded by
import
.
此规则扩展了基本 eslint/no-restricted-imports
规则。 它添加了对类型导入(import type X from "..."
、import { type X } from "..."
)和 import x = require("...")
语法的支持。
英:This rule extends the base eslint/no-restricted-imports
rule. It adds support for the type import (import type X from "..."
, import { type X } from "..."
) and import x = require("...")
syntaxes.
选项
该规则添加了以下选项:
英:This rule adds the following options:
allowTypeImports
(默认:false
)
英:(default: false
)
你可以为特定路径或模式指定此选项,如下所示:
英:You can specify this option for a specific path or pattern as follows:
{
"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
时,规则将允许 仅类型导入。
英:When set to true
, the rule will allow Type-Only Imports.
具有上述配置的代码示例:
英:Examples of code with the above config:
- ❌ 不正确
- ✅ 正确
import foo from 'import-foo';
export { Foo } from 'import-foo';
import baz from 'import-baz';
export { Baz } from 'import-baz';
Open in Playgroundimport { 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';
Open in Playground如何使用
.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
选项。
资源
摘自 ❤️ ESLint 内核