Duplicate identifier 'X'
错误信息
Duplicate identifier
快速摘要
你 declared two 变量, 函数, 类型, or 接口使用相同 name in 相同作用域. TypeScript 做不 允许 duplicate identifiers (except for 接口合并).
为什么发生
Declaring two 变量使用相同 name in 相同作用域
Importing two 不同 things 使用相同 name
函数 and 类型使用相同 name (不使用 overloading)
合并 conflicts bringing in duplicate declarations
最小示例
✗ 错误代码
const name = "Alice";
const name = "Bob"; // duplicate✓ 修复代码
const firstName = "Alice";
const lastName = "Bob";Rename one 的变量到 avoid duplicate identifier.
如何诊断
搜索 for 所有 declarations 的 name in file
检查导入 for naming conflicts
Look for declarations in 包含 files (特别是 .d.ts)
如何修复
Rename one 的 declarations
Use 导入 aliases:
导入 { X as Y }移除 duplicate declaration
For 接口, use declaration 合并 intentionally
如何预防
Use consistent naming 约定
Use 导入 aliases 到 avoid naming conflicts
运行 ESLint 使用否-dupe-类-成员规则