Skip to content

Expected 0 实参, 但获取 1

错误信息

                Expected N 实参, 但获取 M
              

快速摘要

你 called 函数使用更多 or fewer 实参 than 它的 signature declares. TypeScript 检查实参数量 at 编译时间.

为什么发生

  • Passing extra 实参函数 doesn't 接受

  • Forgetting 到通过必需实参

  • 函数 signature doesn't 包含可选/REST 参数

  • Calling 错误函数 overload

最小示例

✗ 错误代码
function greet(name: string): string { return `Hello, ${name}`; }
greet("Alice", 30); // too many args
✓ 修复代码
function greet(name: string, age?: number): string { return `Hello, ${name}`; }
greet("Alice", 30);

添加可选 age 参数到 接受 extra 实参.

如何诊断

  • 检查函数 signature for expected 参数数量

  • Look for 可选参数 (使用 ?) or REST 参数 (...args)

  • 验证你're calling 正确函数 overload

如何修复

  • 移除 extra 实参 or 添加它们到 函数 signature

  • 添加 missing 必需实参

  • 制作参数可选使用 ? or 添加 REST 参数

  • 添加函数 overload 那个接受实参数量你 需要

如何预防

  • Use IDE tooltips 到 see 函数 signatures 之前 calling

  • Document expected 实参使用 JSDoc

相关资源

相关术语

← 返回语言