Skip to main content

no-restricted-imports

Disallow specified modules when loaded by import.

🧱

This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.

This rule extends the base no-restricted-imports rule from ESLint core. 它增加了对类型导入语法的支持:

¥It adds support for type import syntaxes:

  • import type X from "..."

  • import { type X } from "..."

  • import x = require("...")

如何使用

eslint.config.mjs
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"no-restricted-imports": "off",
"@typescript-eslint/no-restricted-imports": "error"
}
});

在线运行试试这个规则 ↗

选项

See eslint/no-restricted-imports's options.

¥Options

该规则添加了以下选项:

¥This rule adds the following options:

allowTypeImports

是否允许路径的仅类型导入。默认:false

¥Whether to allow type-only imports for a path. 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,
},
],
},
],
},
}

是否允许 仅类型导入

¥Whether to 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 Playground

'## 资源'

Taken with ❤️ from ESLint core.