noExplicitAny
Summary
Section titled “Summary”- Rule available since:
v1.0.0
- Diagnostic Category:
lint/suspicious/noExplicitAny
- This rule is recommended, which means is enabled by default.
- This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "suspicious": { "noExplicitAny": "error" } } }}
Description
Section titled “Description”Disallow the any
type usage.
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 prevents an implied any
,
but doesn’t prevent any
from being explicitly used the way this rule does.
Sometimes you can use the type unknown
instead of the type any
.
It also accepts any value, however it requires to check that a property exists before calling it.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”let variable: any = 1;<pre class="language-text"><code class="language-text">code-block.ts:1:15 <a href="https://biomejs.dev/linter/rules/no-explicit-any">lint/suspicious/noExplicitAny</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━<br /><br /> <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected </span><span style="color: Orange;"><strong>any</strong></span><span style="color: Orange;">. Specify a different type.</span><br /> <br /> <strong><span style="color: Tomato;">></span></strong> <strong>1 │ </strong>let variable: any = 1;<br /> <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><br /> <strong>2 │ </strong><br /> <br /> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;"><strong>any</strong></span><span style="color: lightgreen;"> disables many type checking rules. Its use should be avoided.</span><br /> <br /></code></pre>
class SomeClass { message: Array<Array<any>>;}<pre class="language-text"><code class="language-text">code-block.ts:2:25 <a href="https://biomejs.dev/linter/rules/no-explicit-any">lint/suspicious/noExplicitAny</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━<br /><br /> <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected </span><span style="color: Orange;"><strong>any</strong></span><span style="color: Orange;">. Specify a different type.</span><br /> <br /> <strong>1 │ </strong>class SomeClass {<br /> <strong><span style="color: Tomato;">></span></strong> <strong>2 │ </strong> message: Array<Array<any>>;<br /> <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><br /> <strong>3 │ </strong>}<br /> <strong>4 │ </strong><br /> <br /> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;"><strong>any</strong></span><span style="color: lightgreen;"> disables many type checking rules. Its use should be avoided.</span><br /> <br /></code></pre>
function fn(param: Array<any>): void {}<pre class="language-text"><code class="language-text">code-block.ts:1:26 <a href="https://biomejs.dev/linter/rules/no-explicit-any">lint/suspicious/noExplicitAny</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━<br /><br /> <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unexpected </span><span style="color: Orange;"><strong>any</strong></span><span style="color: Orange;">. Specify a different type.</span><br /> <br /> <strong><span style="color: Tomato;">></span></strong> <strong>1 │ </strong>function fn(param: Array<any>): void {}<br /> <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><br /> <strong>2 │ </strong><br /> <br /> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;"><strong>any</strong></span><span style="color: lightgreen;"> disables many type checking rules. Its use should be avoided.</span><br /> <br /></code></pre>
let variable: number = 1;let variable2 = 1;
class SomeClass<T extends any> { message: Array<Array<unknown>>;}
function fn(param: Array<Array<unknown>>): Array<unknown> {}
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.