Skip to content

TS2783: 'X' 是 specified 更多 than 一次

错误信息

                'X' 是 specified 更多 than 一次, 所以这个 usage 将是 overwritten.
              

快速摘要

你有 属性 in 两者都 spread 对象 and explicit assignment. explicit 值 wins, 哪个可能不 是 intended.

为什么发生

  • Spreading 对象 and 然后 overriding 属性使用相同 name

  • Unintentional 属性 duplication in 对象 literals

最小示例

✗ 错误代码
const defaults = { port: 3000, host: 'localhost' };
const config = { ...defaults, port: 8080 }; // TS2783
✓ 修复代码
const defaults = { port: 3000, host: 'localhost' };
const config = { ...defaults, port: 8080 }; // ✅ intentional override

如果 override 是 intentional, 这个是 fine. 否则, 移除 duplicate.

如何诊断

  • 检查哪个属性是 in spread 对象

  • 检查哪个属性是 explicitly 设置之后 spread

如何修复

  • 移除 duplicate 属性如果 spread 值是 intended

  • 移除属性从 spread 如果 explicit 值是 intended

  • 制作 override intentional and document 它使用 comment

← 返回语言