@typescript-eslint/no-explicit-any
Disallow the any type
warn by defaultfixable
Also called typescript-eslint/no-explicit-any, typescript/no-explicit-any
any turns type checking off for a value, and for everything computed from it. You can call it, read any property of it and pass it anywhere, and a typo only shows up when the code runs. unknown also accepts every value, but you have to narrow it before you use it.
This rule is on by default in TypeScript files.
Configure#
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-explicit-any" = "off" # or "error"Every option, set to its default:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-explicit-any" = ["warn", {
fixToUnknown = false,
ignoreRestArgs = false,
}]Examples#
example.ts1 problem
function parse(text: string): anyUnexpected any. Specify a different type. {
return JSON.parse(text);
}
parse("{}").user.nmae; // no type errorexample.tsNo problems
function parse(text: string): unknown {
return JSON.parse(text);
}
const result = parse("{}");
if (typeof result === "string") {
console.log(result.length);
}Options#
| Option | Default | What it does |
|---|---|---|
fixToUnknown | false | Let --fix rewrite any to unknown, and keyof any to PropertyKey. The code may stop compiling until you narrow the values. |
ignoreRestArgs | false | Allow any[], Array<any> and their readonly forms as the type of a rest parameter. |
example.tswith
{ "ignoreRestArgs": true }No problemsfunction log(...args: any[]) {
console.log(...args);
}Every other any is still reported:
example.tswith
{ "ignoreRestArgs": true }1 problemfunction log(message: anyUnexpected any. Specify a different type.) {
console.log(message);
}