@typescript-eslint/no-inferrable-types
Disallow type annotations that repeat what the initial value already says
Also called typescript-eslint/no-inferrable-types, typescript/no-inferrable-types
In const retries: number = 3, TypeScript already infers that retries is a number. The annotation only repeats it.
The rule looks at variables, parameters with a default value and class properties. It reports bigint, boolean, number, null, string, symbol, undefined and RegExp when the value is a literal of that type or a call such as Number(input) or Symbol("id").
A readonly or optional property isn't reported. Without its annotation, readonly limit = 10 has the type 10, not number.
--fix removes the annotation.
Configure#
Off by default. To turn it on:
[lint.rules]
"@typescript-eslint/no-inferrable-types" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"@typescript-eslint/no-inferrable-types" = ["error", {
ignoreParameters = false,
ignoreProperties = false,
}]Examples#
const retries: number = 3Type number trivially inferred from a number literal, remove type annotation.;
const pattern: RegExp = /\d+/Type RegExp trivially inferred from a RegExp literal, remove type annotation.;
function greet(name: string = "world"Type string trivially inferred from a string literal, remove type annotation.) {}
class Cart {
isOpen: boolean = false;Type boolean trivially inferred from a boolean literal, remove type annotation.
}const retries = 3;
const pattern = /\d+/;
function greet(name = "world") {}
class Cart {
isOpen = false;
readonly limit: number = 10;
}An annotation that's wider than the value isn't reported:
let selected: string | null = null;Options#
| Option | Default | What it does |
|---|---|---|
ignoreParameters | false | Don't report parameters. |
ignoreProperties | false | Don't report class properties. |
{ "ignoreParameters": true }No problemsfunction greet(name: string = "world") {}{ "ignoreProperties": true }No problemsclass Cart {
isOpen: boolean = false;
}