Cannot 查找 name 'X'
错误信息
Cannot 查找 name
快速摘要
你 referenced name (变量, 函数, or 类型) 那个 TypeScript cannot 查找 in current 作用域 or 导入.
为什么发生
Typo in 变量 or 函数 name
Forgot 到导入 name 从 another 模块
变量是 declared in 不同作用域
Missing 类型 declaration for 全局变量
最小示例
✗ 错误代码
console.log(userNmae); // typo: not declared✓ 修复代码
const userName = "Alice";
console.log(userName);Declare 变量之前 using 它, and fix 任何 typos.
如何诊断
检查 spelling 的 name
验证导入语句包含 name
检查那个 name 是 declared in accessible 作用域
如果它's 全局, 添加 declaration file (.d.ts)
如何修复
Fix typo 到 match declared name
添加 missing 导入语句
Move declaration 到作用域 accessible 从 usage
添加全局 declaration in .d.ts file
如何预防
Use IDE 使用 autocomplete 到 avoid typos
启用 ESLint 使用否-undef 规则
组织导入 automatically