typescript/no-explicit-any Restriction
What it does
Disallows explicit use of the any
type.
Why is this bad?
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. This rule reports on explicit uses of the any
keyword as a type annotation.
TypeScript's
--noImplicitAny
compiler option prevents an impliedany
, but doesn't preventany
from being explicitly used the way this rule does.
Example
Examples of incorrect code for this rule:
const age: any = "seventeen";
const ages: any[] = ["seventeen"];
const ages: Array<any> = ["seventeen"];
function greet(): any {}
function greet(): any[] {}
function greet(): Array<any> {}
function greet(): Array<Array<any>> {}
function greet(param: Array<any>): string {}
function greet(param: Array<any>): Array<any> {}
Examples of correct code for this rule:
const age: number = 17;
const ages: number[] = [17];
const ages: Array<number> = [17];
function greet(): string {}
function greet(): string[] {}
function greet(): Array<string> {}
function greet(): Array<Array<string>> {}
function greet(param: Array<string>): string {}
function greet(param: Array<string>): Array<string> {}
Options
This rule accepts the following options:
ignoreRestArgs
A boolean to specify if arrays from the rest operator are considered ok. false
by default.
fixToUnknown
Whether to enable auto-fixing in which the any
type is converted to the unknown
type. false
by default.
How to use
To enable this rule in the CLI or using the config file, you can use:
oxlint --deny typescript/no-explicit-any
{
"rules": {
"typescript/no-explicit-any": "error"
}
}