@typescript-eslint/no-restricted-imports

Disallow imports of the modules you list, with an exception for type imports

deprecated

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#

bunfig.toml
[lint.rules]
"@typescript-eslint/no-restricted-imports" = ["error", {
  paths = [{ name = "moment", message = "Use Temporal instead." }],
}]
example.tswith { "paths": [{ "name": "moment", "message": "Use Temporal instead." }] }3 problems
import moment from "moment";
import legacy = require("moment");
export { duration } from "moment";
example.tswith { "paths": [{ "name": "moment", "message": "Use Temporal instead." }] }No problems
import { 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:

KeyWhat it does
nameThe module, exactly as it's written in the import. Required.
messageText added to the report.
importNamesReport only these names. Other names from the module are allowed.
allowImportNamesReport every name except these.
allowTypeImportstrue 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:

KeyWhat it does
groupA list of patterns in .gitignore syntax. A pattern that starts with ! makes an exception.
regexA regular expression, instead of group.
caseSensitivetrue makes group or regex case sensitive. Default false.
importNamePatternReport only the names that match this regular expression.
allowImportNamePatternReport 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:

example.tswith { "paths": [{ "name": "./server/db", "allowTypeImports": true }] }No problems
import type { User } from "./server/db";
import { type Order } from "./server/db";

This one also imports a value, so it's reported:

example.tswith { "paths": [{ "name": "./server/db", "allowTypeImports": true }] }1 problem
import { query, type User } from "./server/db";

With { name: "react", importNames: ["useLayoutEffect"] }, only the first line is reported:

example.tswith { "paths": [{ "name": "react", "importNames": ["useLayoutEffect"] }] }1 problem
import { useLayoutEffect } from "react";
import { useEffect } from "react";

With patterns: [{ group: ["@shop/billing/*", "!@shop/billing/api"] }], only the first line is reported:

example.tswith { "patterns": [{ "group": ["@shop/billing/*", "!@shop/billing/api"] }] }1 problem
import { charge } from "@shop/billing/stripe";
import { pay } from "@shop/billing/api";