noEvolvingTypes
このコンテンツはまだ日本語訳がありません。
Diagnostic Category: lint/suspicious/noEvolvingTypes
Since: v1.6.3
Disallow variables from evolving into any
type through reassignments.
In TypeScript, variables without explicit type annotations can evolve their types based on subsequent assignments.
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.
If you enabled TypeScript’s noImplicitAny and want to benefit of evolving types, then we recommend to disable this rule.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-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.
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.
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.