Skip to content

Types not matching when use destructuring assignment, but the hint error is ignored #49761

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
hxse opened this issue Jul 2, 2022 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@hxse
Copy link

hxse commented Jul 2, 2022

Bug Report

πŸ”Ž Search Terms

Types not matching destructuring assignment hint error ignored

πŸ•— Version & Regression Information

ts version: 4.6.4

⏯ Playground Link

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

πŸ’» Code

type aType = { a: number }
const a: aType = { a: 123, b: 456 } // βœ… hint error 
const b: aType = { a: 123, ...{ b: 456 } }// ❌ not hint error, why?

πŸ™ Actual behavior

const a hint the error
const b not hint the error

πŸ™‚ Expected behavior

const a hint the error
const b hint the error too, beacuse aType don't match "{ b: 456 }"

@hxse hxse changed the title Types not matching is ignored Types not matching when use destructuring assignment, but the hint error is ignored Jul 2, 2022
@Josh-Cena
Copy link
Contributor

See #39998

There's no type-safety loophole here, to say the least... Excess property checks is more of a linting feature.

@hxse
Copy link
Author

hxse commented Jul 2, 2022

let me add tow lines
https://www.typescriptlang.org/play?ssl=7&ssc=1&pln=1&pc=1#code/C4TwDgpgBAhgKuaBeKBvWAuKA7ArgWwCMIAnKAXwFgAoAYwHtsBnYTWBSKFdGLARgBMAZgA0UQlgAsAVgBsFGg2asJ7RFzRtBoqADp96VTPnkF1RYxZRaWdDZwFiZU92tYA7AA4AnGaVWAEyx4dVdeKG0xfV1aMyA

type aType = { a: number }
const a: aType = { a: 123, b: 456 } // βœ… hint error 
const b: aType = { a: 123, ...{ b: 456 } }// ❌ not hint error, why?

const c: { c: number } = { c: 789 }
const d: aType = { a: 123, ...c } // ❌ not hint error, why?

The type of const c is certain, but const d still does not hint the error, Isn't this strange?

@Josh-Cena
Copy link
Contributor

Excess property checks only kick in when the RHS is an object literal, because otherwise there's a non-negligible possibility that you are reusing an object value from elsewhere.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 5, 2022
@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

4 participants