Skip to content

noImplicitAnyLet

诊断类别:lint/suspicious/noImplicitAnyLet

¥Diagnostic Category: lint/suspicious/noImplicitAnyLet

自从:v1.4.0

¥Since: v1.4.0

禁止在变量声明中使用隐式 any 类型。

¥Disallow use of implicit any type on variable declarations.

没有任何类型注释和初始化的 TypeScript 变量声明具有 any 类型。TypeScript 中的任何类型都是类型系统中危险的“逃生舱”。使用 any 会禁用许多类型检查规则,通常最好只作为最后的手段或在原型代码时使用。TypeScript 的 --noImplicitAny 编译器选项 不会报告这种情况。

¥TypeScript variable declaration without any type annotation and initialization have the any type. The any type in TypeScript is a dangerous “escape hatch” from the type system. Using any disables many type checking rules and is generally best used only as a last resort or when prototyping code. TypeScript’s --noImplicitAny compiler option doesn’t report this case.

¥Examples

¥Invalid

var a;
a = 2;
code-block.ts:1:5 lint/suspicious/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable implicitly has the any type.

> 1 │ var a;
^
2 │ a = 2;
3 │

Variable declarations without type annotation and initialization implicitly have the any type. Declare a type or initialize the variable with some value.

let b;
b = 1
code-block.ts:1:5 lint/suspicious/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable implicitly has the any type.

> 1 │ let b;
^
2 │ b = 1
3 │

Variable declarations without type annotation and initialization implicitly have the any type. Declare a type or initialize the variable with some value.

¥Valid

var a = 1;
let a:number;
var b: number
var b =10;

¥Related links