Skip to main content

no-explicit-any

Disallow the any type.

🔧

Some problems reported by this rule are automatically fixable by the --fix ESLint command line option.

💡

Some problems reported by this rule are manually fixable by editor suggestions.

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.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-explicit-any": "error"
}
};
Try this rule in the playground ↗

Examples

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> {}

Options

This rule accepts the following options

type Options = [
{
/** Whether to enable auto-fixing in which the `any` type is converted to the `unknown` type. */
fixToUnknown?: boolean;
/** Whether to ignore rest parameter arrays. */
ignoreRestArgs?: boolean;
},
];

const defaultOptions: Options = [
{ fixToUnknown: false, ignoreRestArgs: false },
];

ignoreRestArgs

A boolean to specify if arrays from the rest operator are considered okay. false by default.

Examples of incorrect code for the { "ignoreRestArgs": false } option:

/*eslint @typescript-eslint/no-explicit-any: ["error", { "ignoreRestArgs": false }]*/

function foo1(...args: any[]): void {}
function foo2(...args: readonly any[]): void {}
function foo3(...args: Array<any>): void {}
function foo4(...args: ReadonlyArray<any>): void {}

declare function bar(...args: any[]): void;

const baz = (...args: any[]) => {};
const qux = function (...args: any[]) {};

type Quux = (...args: any[]) => void;
type Quuz = new (...args: any[]) => void;

interface Grault {
(...args: any[]): void;
}
interface Corge {
new (...args: any[]): void;
}
interface Garply {
f(...args: any[]): void;
}

Examples of correct code for the { "ignoreRestArgs": true } option:

/*eslint @typescript-eslint/no-explicit-any: ["error", { "ignoreRestArgs": true }]*/

function foo1(...args: any[]): void {}
function foo2(...args: readonly any[]): void {}
function foo3(...args: Array<any>): void {}
function foo4(...args: ReadonlyArray<any>): void {}

declare function bar(...args: any[]): void;

const baz = (...args: any[]) => {};
const qux = function (...args: any[]) {};

type Quux = (...args: any[]) => void;
type Quuz = new (...args: any[]) => void;

interface Grault {
(...args: any[]): void;
}
interface Corge {
new (...args: any[]): void;
}
interface Garply {
f(...args: any[]): void;
}

When Not To Use It

If an unknown type or a library without typings is used and you want to be able to specify any.

Further Reading

Resources