Skip to content

noEvolvingTypes

诊断类别:lint/suspicious/noEvolvingTypes

¥Diagnostic Category: lint/suspicious/noEvolvingTypes

自从:v1.6.3 禁止变量通过重新分配演变为 any 类型。

¥Since: v1.6.3 Disallow variables from evolving into any type through reassignments.

在 TypeScript 中,没有显式类型注释的变量可以根据后续赋值演变其类型。

¥In TypeScript, variables without explicit type annotations can evolve their types based on subsequent assignments.

当 TypeScript 的 noImplicitAny 被禁用时,没有显式类型注释的变量隐式具有类型 any。与 any 类型一样,进化的 any 类型禁用了许多类型检查规则,应避免使用以保持强类型安全性。此规则通过确保变量不会演变为 any 类型、鼓励显式类型注释和受控类型演变来防止此类情况。

¥When TypeScript’s noImplicitAny is disabled, variables without explicit type annotations have implicitly the type any. Just like the any type, evolved any types disable many type-checking rules and should be avoided to maintain strong type safety. This rule prevents such cases by ensuring variables do not evolve into any type, encouraging explicit type annotations and controlled type evolutions.

如果你启用了 TypeScript 的 noImplicitAny 并希望从不断发展的类型中受益,那么我们建议禁用此规则。

¥If you enabled TypeScript’s noImplicitAny and want to benefit of evolving types, then we recommend to disable this rule.

¥Examples

¥Invalid

let a;
code-block.ts:1:5 lint/suspicious/noEvolvingTypes ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The type of this variable may evolve implicitly to any type, including the any type.

> 1 │ let a;
^
2 │

Add an explicit type or initialization to avoid implicit type evolution.

const b = [];
code-block.ts:1:7 lint/suspicious/noEvolvingTypes ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The type of this variable may evolve implicitly to any type, including the any type.

> 1 │ const b = [];
^
2 │

Add an explicit type or initialization to avoid implicit type evolution.

let c = null;
code-block.ts:1:5 lint/suspicious/noEvolvingTypes ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The type of this variable may evolve implicitly to any type, including the any type.

> 1 │ let c = null;
^
2 │

Add an explicit type or initialization to avoid implicit type evolution.

¥Valid

let a: number;
let b = 1;
var c : string;
var d = "abn";
const e: never[] = [];
const f = [null];
const g = ['1'];
const h = [1];
let workspace: Workspace | null = null;

¥Related links