Skip to content
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

Unable to catch type error when using spread operator #56927

Closed
richardliu-db opened this issue Jan 2, 2024 · 2 comments
Closed

Unable to catch type error when using spread operator #56927

richardliu-db opened this issue Jan 2, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@richardliu-db
Copy link

richardliu-db commented Jan 2, 2024

πŸ”Ž Search Terms

spread operator

#38657 (comment)

πŸ•— Version & Regression Information

  • typescript version: 5.2.2

⏯ Playground Link

No response

πŸ’» Code

type Type = {
  a: string
}

const foo = ({ a }: Type) => {
  console.log(a)
}

foo({ a: 'hello', b: 'world' }) // Compile error. Expected

foo({ ...{ a: 'hello', b: 'world' } }) // No compile error. Not expected.

πŸ™ Actual behavior

The spread operator can fool the type check, as shown in the line with "No compile error. Not expected.".

πŸ™‚ Expected behavior

The type check should show compile error as b field is not expected

Additional information about the issue

No response

@fatcerberus
Copy link

Duplicate of #39998.

Note that an excess property error is not a type error per se - objects can legally have more properties than the type declares. This is more of a lint check and you shouldn’t rely on it to enforce runtime invariants. See #12936.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 2, 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.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2024
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