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("...")

How to Use

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"
}
});

Try this rule in the playground ↗

Options

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

This rule adds the following options:

allowTypeImports

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

Resources

Taken with ❤️ from ESLint core.