Skip to content

Type is not checked when spreading a typed object into another typed object #58835

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
vsheyanov opened this issue Jun 12, 2024 · 4 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@vsheyanov
Copy link

vsheyanov commented Jun 12, 2024

πŸ”Ž Search Terms

"spread object", "spread typed object"

πŸ•— Version & Regression Information

Tested with TS:

  • 5.0.4
  • 5.4.5

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAbgLhgbxlAptJ0BOBLMAcxgF8YBeFAKBjUyiQHIATRgGipIG4qqB6PjAgALEAFcANsxgAjdDACGYGAFEcOEDiqhIsAqHXpgDFDDzNsUfEVIVqtAHRO4HWuaasO3XgKGiA7hAwACoAyjDo6pra4NAwBjhGJqjuQlYExGSUyDR0WDAs7LmphV5cQA

πŸ’» Code

const v: { test: string } = {
  test: 'd',
};

// should be an Error
const incorrect: { id: string } = {
  ...v,
  id: 'd',
};

// shows TS error
const correct: { id: string } = {
  test: 'd',
  id: 'd',
};

πŸ™ Actual behavior

It allows to spread anything inside the variable const incorrect. You can even spread ...[], inside incorrect variable and it won't throw an error.

πŸ™‚ Expected behavior

If variable v is typed, incorrect case must throw an error.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Another duplicate of #39998. Type checking is working, just excess property checks not.

@vsheyanov
Copy link
Author

@MartinJohns thanks for adding a duplicate. I didn't find that one myself.
I wonder if that issue covers also this case:

// should be an Error
const incorrect: { id: string } = {
  ...[],
  id: 'd',
};

@MartinJohns
Copy link
Contributor

#52060 / #9726.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 13, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "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

4 participants