no-explicit-any
禁止
any
类型.
在 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.
any
的首选替代方案包括:
¥Preferable alternatives to any
include:
-
如果类型已知,则在
interface
或type
中描述它¥If the type is known, describing it in an
interface
ortype
-
如果类型未知,则使用更安全的
unknown
类型¥If the type is not known, using the safer
unknown
type
TypeScript 的
--noImplicitAny
编译器选项可防止隐含的any
,但不会阻止any
按照此规则的方式显式使用。¥TypeScript's
--noImplicitAny
compiler option prevents an impliedany
, but doesn't preventany
from being explicitly used the way this rule does.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-explicit-any": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-explicit-any": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ 错误
- ✅ 正确
const age: any = 'seventeen';
Open in Playgroundconst ages: any[] = ['seventeen'];
Open in Playgroundconst ages: Array<any> = ['seventeen'];
Open in Playgroundfunction greet(): any {}
Open in Playgroundfunction greet(): any[] {}
Open in Playgroundfunction greet(): Array<any> {}
Open in Playgroundfunction greet(): Array<Array<any>> {}
Open in Playgroundfunction greet(param: Array<any>): string {}
Open in Playgroundfunction greet(param: Array<any>): Array<any> {}
Open in Playgroundconst age: number = 17;
Open in Playgroundconst ages: number[] = [17];
Open in Playgroundconst ages: Array<number> = [17];
Open in Playgroundfunction greet(): string {}
Open in Playgroundfunction greet(): string[] {}
Open in Playgroundfunction greet(): Array<string> {}
Open in Playgroundfunction greet(): Array<Array<string>> {}
Open in Playgroundfunction greet(param: Array<string>): string {}
Open in Playgroundfunction greet(param: Array<string>): Array<string> {}
Open in Playground选项
该规则接受以下选项:
type Options = [
{
/** Whether to enable auto-fixing in which the `any` type is converted to the `unknown` type. */
fixToUnknown?: boolean;
/** 是否忽略其余参数数组。 */
ignoreRestArgs?: boolean;
},
];
const defaultOptions: Options = [
{ fixToUnknown: false, ignoreRestArgs: false },
];
¥Options
fixToUnknown
Whether to enable auto-fixing in which the any
type is converted to the unknown
type. Default: false
.
默认情况下,此规则不会提供自动 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
是否忽略其余参数数组。 Default: false
.
以下示例在 {ignoreRestArgs: false}
时不正确,但在 {ignoreRestArgs: true}
时正确。
¥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
始终是一个危险的逃生舱。只要有可能,避免它总是更安全。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
可暂时用于类型尚不清楚或无法表示的地方¥If your project isn't fully onboarded to TypeScript yet,
any
can be temporarily used in places where types aren't yet known or representable -
如果外部包还没有类型,而你想要使用
any
并为其添加.d.ts
¥If an external package doesn't yet have typings and you want to use
any
pending adding a.d.ts
for it -
你正在处理特别复杂或微妙的代码,这些代码尚无法在 TypeScript 类型系统中表示
¥You're working with particularly complex or nuanced code that can't yet be represented in the TypeScript type system
你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
¥You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
相关
¥Related To
进一步阅读
¥Further Reading
-
TypeScript
any
型¥TypeScript
any
type -
TypeScript 的
unknown
型¥TypeScript's
unknown
type -
TypeScript
any
类型文档¥TypeScript
any
type documentation -
TypeScript
unknown
型发行说明¥TypeScript
unknown
type release notes