Skip to content

typescript/no-explicit-any Restriction

🛠️ An auto-fix is available for this rule for some violations.

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 implied any, but doesn't prevent any from being explicitly used the way this rule does.

Example

Examples of incorrect code for this rule:

typescript
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:

typescript
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.

References

Released under the MIT License.