Skip to content

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

Open
@rivatove

Description

@rivatove

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions