@typescript-eslint/no-restricted-types
Disallow the types you list
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:
[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." },
}]{ "types": { "LegacyUser": "Use Account instead." } }1 probleminterface LegacyUser {
name: string;
}
function greet(user: LegacyUserDon't use LegacyUser as a type. Use Account instead.) {
return user.name;
}{ "types": { "LegacyUser": "Use Account instead." } }No problemsinterface 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:
{ "types": { "Record": true } }2 problemslet scores: RecordDon't use Record as a type.<string, number>;
let flags: RecordDon't use Record as a type.<string, boolean>;A key with type arguments matches only those. With the key "Record<string, any>", only the second line is reported:
{ "types": { "Record<string, any>": { "message": "Use unknown.", "fixWith": "Record<string, unknown>" } } }1 problemlet scores: Record<string, number>;
let body: Record<string, any>Don't use Record<string,any> as a type. Use unknown.;Only types are reported, not values with the same name. With { Object: true }, this is allowed:
{ "types": { "Object": true } }No problemsconst keys = Object.keys({ a: 1 });