no-unsafe-call
Disallow calling a value with type
any
.
该规则需要 类型信息 才能运行。
TypeScript 中的 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.
尽管你的意图是好的,但 any
类型有时可能会泄漏到你的代码库中。
将 any
类型的值作为函数调用会在代码库中产生潜在的类型安全漏洞和错误来源。
英:Despite your best intentions, the any
type can sometimes leak into your codebase.
Calling an any
-typed value as a function creates a potential type safety hole and source of bugs in your codebase.
此规则不允许调用任何类型为 any
的值。
英:This rule disallows calling any value that is typed as any
.
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-call": "error"
}
};
示例
- ❌ 不正确
- ✅ 正确
declare const anyVar: any;
declare const nestedAny: { prop: any };
anyVar();
anyVar.a.b();
nestedAny.prop();
nestedAny.prop['a']();
new anyVar();
new nestedAny.prop();
anyVar`foo`;
nestedAny.prop`foo`;
Open in Playgrounddeclare const typedVar: () => void;
declare const typedNested: { prop: { a: () => void } };
typedVar();
typedNested.prop.a();
(() => {})();
new Map();
String.raw`foo`;
Open in Playground何时不使用它
如果你的代码库有许多现有的 any
或不安全代码区域,则可能很难启用此规则。
在项目的不安全区域中,在增加类型安全性之前,跳过 no-unsafe-*
规则可能会更容易。
你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
英:If your codebase has many existing any
s or areas of unsafe code, it may be difficult to enable this rule.
It may be easier to skip the no-unsafe-*
rules pending increasing type safety in unsafe areas of your project.
You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
相关
选项
该规则不可配置。