变量 'X' implicitly 有 '任何' 类型
错误信息
implicitly 有 '任何' 类型
快速摘要
使用 noImplicitAny 启用, TypeScript 错误当 变量 or 参数有 否类型 annotation and 它的类型 cannot 是 inferred, defaulting 到任何.
为什么发生
函数参数不使用类型 annotation
变量 declared 不使用 initialization or annotation
Destructuring 不使用类型 annotations
Using
let不使用值 or 类型
最小示例
✗ 错误代码
function double(x) { return x * 2; } // x implicitly has any✓ 修复代码
function double(x: number): number { return x * 2; }添加类型 annotation 到参数 x.
如何诊断
检查如果变量 or 参数有 类型 annotation
检查如果变量是 initialized 使用值 (for inference)
Look at 错误位置 — 它 names 变量
如何修复
添加 explicit 类型 annotation:
let x: 数字Initialize 变量使用值:
let x = 0For 函数参数, 添加类型 annotations
如果任何是 intentional, use
: 任何explicitly
如何预防
启用 noImplicitAny (部分的 strict 模式)
总是 annotate 函数参数
Initialize 变量当 declaring 它们