Skip to content

Breaking assignment: objects of spread arrays to an array type #43977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oluwasayo opened this issue May 6, 2021 · 2 comments
Closed

Breaking assignment: objects of spread arrays to an array type #43977

oluwasayo opened this issue May 6, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@oluwasayo
Copy link

Bug Report

🔎 Search Terms

  • spread array into object
  • object array

🕗 Version & Regression Information

  • The behavior was observed between 3.5.1 and 4.3.0-beta
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common Bugs that aren't Bugs

⏯ Playground Link

Playground link with relevant code

💻 Code

const arr1 = [{ message: 'blue' }];
const arr2 = [{ subject: 'red' }];

const obj: Array<{ message: string } | { subject: string }> = { ...arr1, ...arr2 };

console.log(obj);

🙁 Actual behavior

Code compiles even though an object is being assigned to an explicitly specified array

🙂 Expected behavior

Compilation should fail. The spread into object does not preserve the semantics of spreading into an array since elements will overwrite each other. This opens up the possibility of a serious data loss.

@RyanCavanaugh
Copy link
Member

Duplicate #35778

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 6, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants