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
.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-unsafe-call": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-call": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
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不安全的 Function
类型
¥The Unsafe Function
Type
Function
类型在调用时的行为几乎与 any
相同,因此此规则也不允许调用 Function
类型的值。
¥The Function
type is behaves almost identically to any
when called, so this rule also disallows calling values of type Function
.
- ❌ Incorrect
const f: Function = () => {};
f();
Open in Playground请注意,虽然 no-unsafe-function-type 有助于防止创建 Function
类型,但此规则有助于防止不安全地使用 Function
类型,这可能会潜入你的代码库而无需明确引用 Function
类型。例如,请参阅以下代码:
¥Note that whereas no-unsafe-function-type helps prevent the creation of Function
types, this rule helps prevent the unsafe use of Function
types, which may creep into your codebase without explicitly referencing the Function
type at all.
See, for example, the following code:
function callUnsafe(maybeFunction: unknown): string {
if (typeof maybeFunction === 'function') {
// TypeScript allows this, but it's completely unsound.
return maybeFunction('call', 'with', 'any', 'args');
}
// etc
}
在这种情况下,请注意无法通过运行时检查来保证值可 以安全调用。如果你真的想调用一个你不知道其类型的值,最好的方法是使用 try
/catch
并抑制任何妨碍你的 TypeScript 或 linter 错误。
¥In this sort of situation, beware that there is no way to guarantee with runtime checks that a value is safe to call.
If you really want to call a value whose type you don't know, your best best is to use a try
/catch
and suppress any TypeScript or linter errors that get in your way.
function callSafe(maybeFunction: unknown): void {
try {
// intentionally unsound type assertion
(maybeFunction as () => unknown)();
} catch (e) {
console.error(
'Function either could not be called or threw an error when called: ',
e,
);
}
}
选项
该规则不可配置。
何时不使用它
¥When Not To Use It
如果你的代码库有许多现有的 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.
相关
¥Related To
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.
'## 资源'