Skip to content

noInferrableTypes

诊断类别:lint/style/noInferrableTypes

¥Diagnostic Category: lint/style/noInferrableTypes

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止使用文字表达式初始化的变量、参数和类属性的类型注释。

¥Disallow type annotations for variables, parameters, and class properties initialized with a literal expression.

TypeScript 能够从参数、属性和变量的默认值或初始值推断出它们的类型。对于简单推断的类型(boolean、bigint、number、regex、string),无需使用显式 : 类型注释。这样做会给代码增加不必要的冗长,使其更难阅读。

¥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.

与 ESLint 的规则相反,此规则允许对 const 声明使用宽类型。此外,该规则不识别 undefined 值、原始类型构造函数(String、Number 等)和 RegExp 类型。这些全局变量可以被本地变量遮蔽。

¥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.

¥Examples

¥Invalid

const variable: 1 = 1;
code-block.ts: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;
code-block.ts: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;
}
code-block.ts: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;
}
code-block.ts: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 {}
code-block.ts: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

¥Valid

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 {}

¥Related links