@typescript-eslint/no-inferrable-types

Disallow type annotations that repeat what the initial value already says

fixable

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:

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

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-inferrable-types" = ["error", {
  ignoreParameters = false,
  ignoreProperties = false,
}]

Examples#

example.ts4 problems
const retries: number = 3;
const pattern: RegExp = /\d+/;

function greet(name: string = "world") {}

class Cart {
  isOpen: boolean = false;
}
example.tsNo problems
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:

example.tsNo problems
let selected: string | null = null;

Options#

OptionDefaultWhat it does
ignoreParametersfalseDon't report parameters.
ignorePropertiesfalseDon't report class properties.
example.tswith { "ignoreParameters": true }No problems
function greet(name: string = "world") {}
example.tswith { "ignoreProperties": true }No problems
class Cart {
  isOpen: boolean = false;
}