Skip to content
← Back to rules

typescript/no-unsafe-call Pedantic

💭 This rule requires type information.

它的作用

此规则禁止调用类型为 any 的值。

为什么这不好?

TypeScript 中的 any 类型会禁用类型检查。当你调用一个被类型标注为 any 的值时,TypeScript 无法验证它实际上是否是一个函数、它期望哪些参数,或者它会返回什么。这可能导致运行时错误。

示例

此规则的错误代码示例:

ts
declare const anyValue: any;

anyValue(); // 不安全的调用

anyValue(1, 2, 3); // 不安全的调用

const result = anyValue("hello"); // 不安全的调用

// 链式不安全调用
anyValue().then().catch(); // 不安全

此规则的正确代码示例:

ts
declare const fn: () => void;
declare const fnWithParams: (a: number, b: string) => boolean;
declare const unknownValue: unknown;

fn(); // 安全

const result = fnWithParams(1, "hello"); // 安全

// unknown 的类型守卫
if (typeof unknownValue === "function") {
  unknownValue(); // 在类型守卫后是安全的
}

// 如果你确定,可以显式类型断言
(anyValue as () => void)(); // 明确不安全,但这是有意为之

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-unsafe-call": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/no-unsafe-call": "error",
  },
});
bash
oxlint --type-aware --deny typescript/no-unsafe-call

版本

此规则已在 v1.12.0 中添加。

参考资料