@typescript-eslint/no-restricted-types

Disallow the types you list

fixable

Also called typescript-eslint/no-restricted-types, typescript/no-restricted-types

Some types shouldn't be used in your project, like a deprecated type, one that's easy to confuse with another, or one the project has a better name for. You list them, and the rule reports every use as a type.

The rule does nothing until you give it options.

--fix replaces a type if you set fixWith for it.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-restricted-types" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"@typescript-eslint/no-restricted-types" = ["error", {
  # types = <object>,
}]

Examples#

bunfig.toml
[lint.rules]
"@typescript-eslint/no-restricted-types" = ["error", {
  types = { LegacyUser = "Use Account instead." },
}]
example.tswith { "types": { "LegacyUser": "Use Account instead." } }1 problem
interface LegacyUser {
  name: string;
}

function greet(user: LegacyUser) {
  return user.name;
}
example.tswith { "types": { "LegacyUser": "Use Account instead." } }No problems
interface Account {
  name: string;
}

function greet(user: Account) {
  return user.name;
}

Options#

types is an object. Each key is a type as you write it in code, and spaces don't matter. It can be a name (LegacyUser, NodeJS.Timeout), a name with type arguments (Array<any>), a keyword other than any (object, null, unknown), or the empty {} and [].

ValueWhat it does
trueReport the type.
A stringReport the type, with this text after the message.
{ "message": .., "fixWith": .., "suggest": [..] }message is the text. fixWith is what --fix writes in its place. suggest lists replacements that an editor offers. All three are optional.

A name matches no matter what its type arguments are. With { Record: true }, both lines are reported:

example.tswith { "types": { "Record": true } }2 problems
let scores: Record<string, number>;
let flags: Record<string, boolean>;

A key with type arguments matches only those. With the key "Record<string, any>", only the second line is reported:

example.tswith { "types": { "Record<string, any>": { "message": "Use unknown.", "fixWith": "Record<string, unknown>" } } }1 problem
let scores: Record<string, number>;
let body: Record<string, any>;

Only types are reported, not values with the same name. With { Object: true }, this is allowed:

example.tswith { "types": { "Object": true } }No problems
const keys = Object.keys({ a: 1 });