Microsoft/TypeScript

Destructuring into an empty object vs an object with existing properties yields different results

Open

#60,386 创建于 2024年10月31日

在 GitHub 查看
 (3 评论) (1 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: check: Excess Property CheckingHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

🔎 Search Terms

"destructuring", "record"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.3#code/MYewdgzgLgBATgU1HAJgdQJZQBYGUpwZgDmEAXDAEpIioA80hJANDI0cQHwwC8MA3gF8A3AChRCAB4AHWrFCRYiZOizYAcgFcAtgCMEcclRr12LGGB3643Pv1EwYUggEMACnBDSDUAJ4UABmYHGAA6cOVaVRx8JlJgkSA

💻 Code

This raises a type error:

const recordWithStrings: Record<string, string> = {};

export const recordWithNumbers: Record<string, number> = {
  ...recordWithStrings,
};

and this doesn't:

const recordWithStrings: Record<string, string> = {};

export const recordWithNumbers: Record<string, number> = {
  extraProperty: 0,
  ...recordWithStrings,
};

Is there a sound reason for that?

🙁 Actual behavior

The 2nd example doesn't raise a type error.

🙂 Expected behavior

The 2nd example should raise a type error.

Additional information about the issue

No response

贡献者指南