Skip to main content

no-explicit-any

Disallow the any type.

ESLint 配置 中扩展"plugin:@typescript-eslint/recommended" 可启用此规则。

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复

💡

此规则报告的一些问题可以通过编辑器 建议 手动修复。


TypeScript 中的 any 类型是类型系统中危险的 "应急方案"。 使用 any 会禁用许多类型检查规则,通常最好仅作为最后手段或在构建代码原型时使用。 此规则报告显式使用 any 关键字作为类型注释。

英: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 的 --noImplicitAny 编译器选项会阻止隐含的 any,但不会阻止按照此规则显式使用 any

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-explicit-any": "error"
}
};
在线运行试试这个规则 ↗

示例

const age: any = 'seventeen';
Open in Playground
const ages: any[] = ['seventeen'];
Open in Playground
const ages: Array<any> = ['seventeen'];
Open in Playground
function greet(): any {}
Open in Playground
function greet(): any[] {}
Open in Playground
function greet(): Array<any> {}
Open in Playground
function greet(): Array<Array<any>> {}
Open in Playground
function greet(param: Array<any>): string {}
Open in Playground
function greet(param: Array<any>): Array<any> {}
Open in Playground

选项

fixToUnknown

默认情况下,此规则不会提供自动 ESLint 修复: 仅选择加入建议。 将类型切换到 unknown 更安全,但可能会导致其他类型错误。

英: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.

启用 { "fixToUnknown": true } 将为规则提供一个自动修复程序,以将 : any 替换为 : unknown

英:Enabling { "fixToUnknown": true } gives the rule an auto-fixer to replace : any with : unknown.

ignoreRestArgs

一个布尔值,用于指定来自剩余运算符的数组是否被认为可以。 默认为 false

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

下面的例子是 {ignoreRestArgs: false}incorrect{ignoreRestArgs: true}correct

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

何时不使用它

any 始终是一个危险的应急方案。 只要有可能,避免它总是更安全。 TypeScript 的 unknown 几乎总是优于 any

英: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.

然而,在某些情况下可能需要使用 any。 最常见的:

英:However, there are occasional situations where it can be necessary to use any. Most commonly:

  • 如果你的项目尚未完全加入 TypeScript,可以在类型未知或无法表示的地方临时使用 any
  • 如果外部包还没有类型并且你想使用 any 并为其添加 .d.ts
  • 你正在处理特别复杂或微妙的代码,这些代码尚无法在 TypeScript 类型系统中表示

你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

英:You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

进一步阅读

选项

该规则接受以下选项

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 },
];

资源