-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix destructuring with fallback #31711
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
Conversation
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 0895fd6. You can monitor the build here. It should now contribute to this PR's status checks. |
Heya @ahejlsberg, I've started to run the extended test suite on this PR at 0895fd6. You can monitor the build here. It should now contribute to this PR's status checks. |
Will this also work with non-optional properties/elements? function f1(options?: { color: string, width: number }) {
let { color, width } = options || {}; // does this result in 'color: string | undefined' and 'width: number | undefined'?
}
function f2(options?: [string, number]) {
let [str, num] = options || [];
} |
We need a few more tweaks to make that work. We currently consider |
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 48d343b. You can monitor the build here. It should now contribute to this PR's status checks. |
Heya @ahejlsberg, I've started to run the extended test suite on this PR at 48d343b. You can monitor the build here. It should now contribute to this PR's status checks. |
I don't think that's right? The protection here is not just for runtime error, but also for typos. As @RyanCavanaugh implied in his comment, the intention is to catch errors like this: function getColorOption(options: { color: string }) {
return (options || {}).colour;
} |
To clarify: this change is brilliant and very helpful. Thanks a lot!! Allowing the same for non-optional fields is what sounds wrong to me. |
Your example with the misspelled property is still an error (the function getColorOption(options?: { color: string }) {
return (options || {}).color;
} Here we now add |
@ahejlsberg it's actually possible for typos to sneak into the fallback object literal: function getColorOption(options?: { color: string }) {
return (options || {colour: 'red'}).color;
} Would it make sense to use the LHS type as contextual type for the RHS and do excess property checking? Or would that break all other uses of fallback objects? |
This is a breaking change in prettier. See #31762 for details. |
With this PR we properly permit destructurings to specify fallbacks:
Previously we would complain that "Initializer provides no value for this binding element and the binding element has no default value".
Fixes #26235.