@typescript-eslint/no-restricted-imports
Disallow imports of the modules you list, with an exception for type imports
Also called typescript-eslint/no-restricted-imports, typescript/no-restricted-imports
Some modules shouldn't be imported in your project, or not from everywhere, like a library you're moving away from, another package's internals, or server code in the browser bundle. You list them, and the rule reports every import, export .. from and import x = require("..") of one.
The rule does nothing until you give it options. Like typescript-eslint, it doesn't check require() or import(). With an oxlint config it does check import(), because oxlint's version of the rule does.
This rule is deprecated. Use no-restricted-imports instead. It has the same options, including allowTypeImports.
Examples#
[lint.rules]
"@typescript-eslint/no-restricted-imports" = ["error", {
paths = [{ name = "moment", message = "Use Temporal instead." }],
}]{ "paths": [{ "name": "moment", "message": "Use Temporal instead." }] }3 problemsimport moment from "moment";moment import is restricted from being used. Use Temporal instead.
import legacy = require("moment");moment import is restricted from being used. Use Temporal instead.
export { duration } from "moment";moment import is restricted from being used. Use Temporal instead.{ "paths": [{ "name": "moment", "message": "Use Temporal instead." }] }No problemsimport { format } from "./dates";
const moment = await import("moment");Options#
Pass either a list of paths entries, such as ["error", "moment", "lodash"], or one object with paths, patterns or both.
Each paths entry is a module name or an object:
| Key | What it does |
|---|---|
name | The module, exactly as it's written in the import. Required. |
message | Text added to the report. |
importNames | Report only these names. Other names from the module are allowed. |
allowImportNames | Report every name except these. |
allowTypeImports | true allows import type and export type from the module. Default false. |
patterns is a list of patterns in .gitignore syntax, or a list of objects. An object takes the keys message, importNames, allowImportNames and allowTypeImports from above, plus these:
| Key | What it does |
|---|---|
group | A list of patterns in .gitignore syntax. A pattern that starts with ! makes an exception. |
regex | A regular expression, instead of group. |
caseSensitive | true makes group or regex case sensitive. Default false. |
importNamePattern | Report only the names that match this regular expression. |
allowImportNamePattern | Report every name except those that match this regular expression. |
An import type is erased when the file is compiled, so it doesn't load anything. With { name: "./server/db", allowTypeImports: true }, these are allowed:
{ "paths": [{ "name": "./server/db", "allowTypeImports": true }] }No problemsimport type { User } from "./server/db";
import { type Order } from "./server/db";This one also imports a value, so it's reported:
{ "paths": [{ "name": "./server/db", "allowTypeImports": true }] }1 problemimport { query, type User } from "./server/db";./server/db import is restricted from being used.With { name: "react", importNames: ["useLayoutEffect"] }, only the first line is reported:
{ "paths": [{ "name": "react", "importNames": ["useLayoutEffect"] }] }1 problemimport { useLayoutEffectuseLayoutEffect import from react is restricted. } from "react";
import { useEffect } from "react";With patterns: [{ group: ["@shop/billing/*", "!@shop/billing/api"] }], only the first line is reported:
{ "patterns": [{ "group": ["@shop/billing/*", "!@shop/billing/api"] }] }1 problemimport { charge } from "@shop/billing/stripe";@shop/billing/stripe import is restricted from being used by a pattern.
import { pay } from "@shop/billing/api";