Title: Cascade type narrowing to child constants · Issue #53202 · microsoft/TypeScript · GitHub
Open Graph Title: Cascade type narrowing to child constants · Issue #53202 · microsoft/TypeScript
X Title: Cascade type narrowing to child constants · Issue #53202 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms narrow child variable related ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of ex...
Open Graph Description: Suggestion 🔍 Search Terms narrow child variable related ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wo...
X Description: Suggestion 🔍 Search Terms narrow child variable related ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code Thi...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/53202
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cascade type narrowing to child constants","articleBody":"# Suggestion\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n## 🔍 Search Terms\r\n\r\n\u003c!--\r\n 💡 Did you know? TypeScript has over 2,000 open suggestions!\r\n 🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.\r\n The \"Common Feature Requests\" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests\r\n\r\n Replace the text below:\r\n-->\r\n\r\nnarrow child variable related\r\n\r\n## ✅ Viability Checklist\r\n\r\n\u003c!--\r\n Suggestions that don't meet all these criteria are very, very unlikely to be accepted.\r\n We always recommend reviewing the TypeScript design goals before investing time writing\r\n a proposal for ideas outside the scope of the project.\r\n-->\r\nMy suggestion meets these guidelines:\r\n\r\n* [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [X] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [X] This could be implemented without emitting different JS based on the types of the expressions\r\n* [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [X] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion\r\n\r\nConstants that were assigned a child value of another variable or constant should have their type narrowed alongside their parent.\r\n\r\n## 📃 Motivating Example\r\n\r\nConsider the following code:\r\n```ts\r\ntype SomeUnion = (\r\n | {type: \"string\", values: readonly string[]}\r\n | {type: \"number\", values: readonly number[]}\r\n | {type: \"date\", values: readonly Date[]}\r\n);\r\n\r\nfunction someFunction(union:SomeUnion, index:number): void {\r\n const value = union.values[index];\r\n if (union.type === \"string\") {\r\n union.values; // readonly string[]\r\n value; // string | number | Date\r\n }\r\n}\r\n```\r\n\r\nIn `someFunction`, TypeScript keeps the type of `value` as it was originally assigned (`string | number | Date`).\r\n\r\nWhat if dev expects `value` to have been narrowed to `string`?\r\n\r\nThis already happens with the root keys of an object\r\n```ts\r\ntype AnotherUnion = (\r\n | {type: \"string\", sibling: string, foo: {bar: string}}\r\n | {type: \"number\", sibling: number, foo: {bar: number}}\r\n | {type: \"date\", sibling: Date, foo: {bar: Date}}\r\n);\r\n\r\nfunction manualDestructure(union: AnotherUnion): void {\r\n const {type, sibling, foo: {bar}} = union;\r\n if (type === \"string\") {\r\n sibling; // string ✔️ \r\n bar; // string | number | Date 😿 \r\n }\r\n}\r\n\r\nfunction argumentDestructure({type, sibling, foo: {bar}}: AnotherUnion): void {\r\n if (type === \"string\") {\r\n sibling; // string ✔️ \r\n bar; // string | number | Date 😿 \r\n }\r\n}\r\n```\r\n\r\nThis is observable in both examples.\r\n\r\n## 💻 Use Cases\r\n\r\nUse case: Working with discriminated unions holding nested objects and arrays\r\n\r\nTo achieve such narrowing, one currently must adjust coding style. ie:\r\n```ts\r\nfunction adjustedScope(union:SomeUnion, index:number): void {\r\n if (union.type === \"string\") {\r\n const value = union.values[index];\r\n value; // string\r\n } else if (union.type === \"number\") {\r\n const value = union.values[index]; // 😭 \r\n value; // number\r\n }\r\n}\r\n\r\n// or\r\n\r\nfunction typeAssertion(union:SomeUnion, index:number): void {\r\n const value = union.values[index];\r\n if (union.type === \"string\") {\r\n (value as string); // string\r\n } else if (union.type === \"number\") {\r\n (value as number); // number 😭 & risky?\r\n }\r\n}\r\n```\r\n","author":{"url":"https://github.com/drakuno","@type":"Person","name":"drakuno"},"datePublished":"2023-03-10T22:38:27Z","url":"https://github.com/microsoft/TypeScript/issues/53202"}
| 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:4451da26-3a77-0e9d-cdec-9a62c6730aac |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CAA6:157C65:D5A2C2:11DFD2E:6B010263 |
| html-safe-nonce | 92b216a2ce345ef688cc1572b1dde6d8a7fd20d2225f9fd0ec2d1a84e446562d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUE2OjE1N0M2NTpENUEyQzI6MTFERkQyRTo2QjAxMDI2MyIsInZpc2l0b3JfaWQiOiIzNzU1NDM2Mzc5NDA0ODI5Mjg0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9e6af54b97d75214540600e275cd1550017a7eccc52a05c3352682d986202673 |
| hovercard-subject-tag | issue:1619710190 |
| 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/53202/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cdd3d0b87f3c534ab2a0bdbbc9103d3a379b2630ade16ea41809d50817e3148c/microsoft/TypeScript/issues/53202 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cdd3d0b87f3c534ab2a0bdbbc9103d3a379b2630ade16ea41809d50817e3148c/microsoft/TypeScript/issues/53202 |
| og:image:alt | Suggestion 🔍 Search Terms narrow child variable related ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | drakuno |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width