-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Default destructuring object not recognized properly. #52049
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
Comments
Note that this is the best DRY way to define this in javascript. If you leave out the |
Duplicate of #51179. |
Ah I see, the correct way of writing this is:
I assume this is because the type |
Yeah, for destructurings, the type annotation specifies what kind of values can be used to initialize it, not what types the bindings will ultimately have. It usually happens to be that these are the exact same type, except for the case where one or more of the bindings has a default. The way to think about it is that you’re annotating the imaginary temp variable used to destructure the object. It’s a bit weird IMO, but that’s how they chose to design it. |
Yeah, among equal desugarings to choose from, TS treats this as function f(_obj: {x: number} = {}) { // <- type violation
let x = _obj.x === undefined ? 1 : _obj.x;
return x;
} |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
destrucruting, object, default, empty
🕗 Version & Regression Information
Version 4.9.4
⏯ Playground Link
https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMAFAbwB6ILyIIwC+AXIpiWCALYBGApgE4E6kECUpAUIt4vbVCHpIMHAkA
💻 Code
🙁 Actual behavior
Property 'x' is missing in type '{}' but required in type '{ x: number; }'
This is wrong because calling, for example,
f()
will result inx
being1
, even tho the= {}
default definition is empty (because the destructuring object has a default).🙂 Expected behavior
I would expect this to pass type linting.
The text was updated successfully, but these errors were encountered: