Disallow the
any
type.
💡
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.
Preferable alternatives to any
include:
interface
or type
unknown
typeTypeScript's
--noImplicitAny
compiler option prevents an impliedany
, but doesn't preventany
from being explicitly used the way this rule does.
eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-explicit-any": "error"
}
});
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-explicit-any": "error"
}
};
Try this rule in the playground ↗
Examples​ Options​This rule accepts the following options:
type Options = [
{
fixToUnknown?: boolean;
ignoreRestArgs?: boolean;
},
];
const defaultOptions: Options = [
{ fixToUnknown: false, ignoreRestArgs: false },
];
fixToUnknown
​
Whether to enable auto-fixing in which the any
type is converted to the unknown
type. Default: false
.
By default, this rule will not provide automatic ESLint fixes: only opt-in suggestions. Switching types to unknown
is safer but is likely to cause additional type errors.
Enabling { "fixToUnknown": true }
gives the rule an auto-fixer to replace : any
with : unknown
.
ignoreRestArgs
​
Whether to ignore rest parameter arrays. Default: false
.
The examples below are incorrect when {ignoreRestArgs: false}
, but correct when {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;
}
Open in Playground When Not To Use It​
any
is always a dangerous escape hatch. Whenever possible, it is always safer to avoid it. TypeScript's unknown
is almost always preferable to any
.
However, there are occasional situations where it can be necessary to use any
. Most commonly:
any
can be temporarily used in places where types aren't yet known or representableany
pending adding a .d.ts
for itYou might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
any
s with Linting and TypeScriptno-unsafe-argument
no-unsafe-assignment
no-unsafe-call
no-unsafe-member-access
no-unsafe-return
any
typeunknown
typeany
type documentationunknown
type release notesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4