src/content/docs/linter/rules/no-inferrable-types.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="TypeScript and TSX" icon="seti:typescript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/style/noInferrableTypes`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`@typescript-eslint/no-inferrable-types`](https://typescript-eslint.io/rules/no-inferrable-types){
"linter": {
"rules": {
"style": {
"noInferrableTypes": "error"
}
}
}
}
Disallow type annotations for variables, parameters, and class properties initialized with a literal expression.
TypeScript is able to infer the types of parameters, properties, and variables from their default or initial values.
There is no need to use an explicit : type annotation for trivially inferred types (boolean, bigint, number, regex, string).
Doing so adds unnecessary verbosity to code making it harder to read.
In contrast to ESLint's rule, this rule allows to use a wide type for const declarations.
Moreover, the rule does not recognize undefined values, primitive type constructors (String, Number, ...), and RegExp type.
These global variables could be shadowed by local ones.
const variable: 1 = 1;
let variable: number = 1;
class SomeClass {
readonly field: 1 = 1;
}
class SomeClass {
field: number = 1;
}
function f(param: number = 1): void {}
const variable: number = 1;
let variable: 1 | 2 = 1;
class SomeClass {
readonly field: number = 1;
}
// `undefined` could be shadowed
const variable: undefined = undefined;
// `RegExp` could be shadowed
const variable: RegExp = /a/;
// `String` could be shadowed
let variable: string = String(5);
class SomeClass {
field: 1 | 2 = 1;
}
function f(param: 1 | 2 = 1): void {}