Skip to content

noInferrableTypes (since v1.0.0)

Diagnostic Category: lint/style/noInferrableTypes

Sources:

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;
style/noInferrableTypes.js:1:15 lint/style/noInferrableTypes  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This type annotation is trivially inferred from its initialization.
  
  > 1 │ const variable: 1 = 1;
                 ^^^
    2 │ 
  
   Safe fix: Remove the type annotation.
  
    1  - const·variable:·1·=·1;
      1+ const·variable·=·1;
    2 2  
  
let variable: number = 1;
style/noInferrableTypes.js:1:13 lint/style/noInferrableTypes  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This type annotation is trivially inferred from its initialization.
  
  > 1 │ let variable: number = 1;
               ^^^^^^^^
    2 │ 
  
   Safe fix: Remove the type annotation.
  
    1  - let·variable:·number·=·1;
      1+ let·variable·=·1;
    2 2  
  
class SomeClass {
readonly field: 1 = 1;
}
style/noInferrableTypes.js:2:17 lint/style/noInferrableTypes  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This type annotation is trivially inferred from its initialization.
  
    1 │ class SomeClass {
  > 2 │   readonly field: 1 = 1;
                   ^^^
    3 │ }
    4 │ 
  
   Safe fix: Remove the type annotation.
  
    1 1  class SomeClass {
    2  - ··readonly·field:·1·=·1;
      2+ ··readonly·field·=·1;
    3 3  }
    4 4  
  
class SomeClass {
field: number = 1;
}
style/noInferrableTypes.js:2:8 lint/style/noInferrableTypes  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This type annotation is trivially inferred from its initialization.
  
    1 │ class SomeClass {
  > 2 │   field: number = 1;
          ^^^^^^^^
    3 │ }
    4 │ 
  
   Safe fix: Remove the type annotation.
  
    1 1  class SomeClass {
    2  - ··field:·number·=·1;
      2+ ··field·=·1;
    3 3  }
    4 4  
  
function f(param: number = 1): void {}
style/noInferrableTypes.js:1:17 lint/style/noInferrableTypes  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This type annotation is trivially inferred from its initialization.
  
  > 1 │ function f(param: number = 1): void {}
                   ^^^^^^^^
    2 │ 
  
   Safe fix: Remove the type annotation.
  
    1  - function·f(param:·number·=·1):·void·{}
      1+ function·f(param·=·1):·void·{}
    2 2  
  
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 {}