Title: Error when destructuring with literal initializers as fallback · Issue #26235 · microsoft/TypeScript · GitHub
Open Graph Title: Error when destructuring with literal initializers as fallback · Issue #26235 · microsoft/TypeScript
X Title: Error when destructuring with literal initializers as fallback · Issue #26235 · microsoft/TypeScript
Description: TypeScript Version: 3.0.1 Destructuring with a fallback empty object literal is a common practice, especially in function arguments (which works), but not always: Code type Options = { color?: string; width?: number; }; function A({ colo...
Open Graph Description: TypeScript Version: 3.0.1 Destructuring with a fallback empty object literal is a common practice, especially in function arguments (which works), but not always: Code type Options = { color?: stri...
X Description: TypeScript Version: 3.0.1 Destructuring with a fallback empty object literal is a common practice, especially in function arguments (which works), but not always: Code type Options = { color?: stri...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26235
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Error when destructuring with literal initializers as fallback","articleBody":"**TypeScript Version:** 3.0.1\r\n\r\nDestructuring with a fallback empty object literal is a common practice, especially in function arguments (which works), but not always:\r\n\r\n**Code**\r\n```ts\r\ntype Options = { color?: string; width?: number; };\r\n\r\nfunction A({ color, width }: Options = {}) {\r\n //\r\n}\r\n\r\nfunction B(options: Options) {\r\n options = options || {};\r\n let { color, width } = options;\r\n}\r\n\r\nfunction C(options: Options) {\r\n let { color, width } = options || {};\r\n}\r\n```\r\n\r\n**Expected behavior:** All three functions type-check and behave the same.\r\n\r\n**Actual behavior:** \"Initializer provides no value for this binding element\" Error.\r\n\r\nWhich is simply incorrect, ES spec clearly defines the value when an object key is missing as `undefined`. Furthermore, I would argue TS should even accept `let { color } = {};` though it's obviously not as common/important.\r\n\r\n**Related Issues:** \r\n[Issue #4598 - Improved checking of destructuring with literal initializers](4598) fixed the same problem, but only for destructuring function arguments (example A).\r\n\r\n**Playground Link:** [here](https://www.typescriptlang.org/play/#src=%0D%0Atype%20Options%20%3D%20%7B%20color%3F%3A%20string%3B%20width%3F%3A%20number%3B%20%7D%3B%0D%0A%0D%0Afunction%20A(%7B%20color%2C%20width%20%7D%3A%20Options%20%3D%20%7B%7D)%20%7B%0D%0A%20%20%20%20%2F%2F%0D%0A%7D%0D%0A%0D%0Afunction%20B(options%3A%20Options)%20%7B%0D%0A%20%20%20%20options%20%3D%20options%20%7C%7C%20%7B%7D%3B%0D%0A%20%20%20%20let%20%7B%20color%2C%20width%20%7D%20%3D%20options%3B%0D%0A%7D%0D%0A%0D%0Afunction%20C(options%3A%20Options)%20%7B%0D%0A%20%20%20%20let%20%7B%20color%2C%20width%20%7D%20%3D%20options%20%7C%7C%20%7B%7D%3B%0D%0A%7D%0D%0A)","author":{"url":"https://github.com/zombie","@type":"Person","name":"zombie"},"datePublished":"2018-08-06T16:53:47Z","url":"https://github.com/microsoft/TypeScript/issues/26235"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9e1c8d54-839f-5456-12ee-002bdbdb6b02 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C376:B1B84:8D410:B1C79:6B00F59E |
| html-safe-nonce | e629be1a32667ab0ac3fde635a3c847a6d5386a9167ff4d37bfcf02047d23347 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzc2OkIxQjg0OjhENDEwOkIxQzc5OjZCMDBGNTlFIiwidmlzaXRvcl9pZCI6IjIwODI4NzUyMjk4NTU2MTAyNzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 125edee537a1d28b8ed73fbb6716abe4af40594e4521a59a7aa0c6ddc5e40048 |
| hovercard-subject-tag | issue:348002194 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/26235/issue_layout |
| twitter:image | https://opengraph.githubassets.com/90e7277b495a0220a1b947b370f408aab7e690395ee412e1600fa98695c03824/microsoft/TypeScript/issues/26235 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/90e7277b495a0220a1b947b370f408aab7e690395ee412e1600fa98695c03824/microsoft/TypeScript/issues/26235 |
| og:image:alt | TypeScript Version: 3.0.1 Destructuring with a fallback empty object literal is a common practice, especially in function arguments (which works), but not always: Code type Options = { color?: stri... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zombie |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width