Linter · Rules
@typescript-eslint/no-restricted-types
Disallow the types you list
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:
[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.
[lint.rules]
"@typescript-eslint/no-restricted-types" = ["error", {
# types = <object>,
}]
Examples
[lint.rules]
"@typescript-eslint/no-restricted-types" = ["error", {
types = { LegacyUser = "Use Account instead." },
}]
Reported:
interface LegacyUser {
name: string;
}
function greet(user: LegacyUser) {
return user.name;
}
Not reported:
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 [].
| Value | What it does |
|---|---|
true | Report the type. |
| A string | Report 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:
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:
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:
const keys = Object.keys({ a: 1 });