Title: Future proof union to intersection type conversion · Issue #29594 · microsoft/TypeScript · GitHub
Open Graph Title: Future proof union to intersection type conversion · Issue #29594 · microsoft/TypeScript
X Title: Future proof union to intersection type conversion · Issue #29594 · microsoft/TypeScript
Description: Search Terms union to intersection, type merge Suggestion I'd like to either standardize (meaning documented behavior) or have a less hacky (future proof) way of transforming union types to intersections. Use Cases The most common use ca...
Open Graph Description: Search Terms union to intersection, type merge Suggestion I'd like to either standardize (meaning documented behavior) or have a less hacky (future proof) way of transforming union types to interse...
X Description: Search Terms union to intersection, type merge Suggestion I'd like to either standardize (meaning documented behavior) or have a less hacky (future proof) way of transforming union types to int...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/29594
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Future proof union to intersection type conversion","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Please read the FAQ first, especially the \"Common Feature Requests\" section.\r\n\r\n-->\r\n\r\n## Search Terms\r\n\r\nunion to intersection, type merge\r\n\r\n\u003c!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->\r\n\r\n## Suggestion\r\n\r\nI'd like to either standardize (meaning documented behavior) or have a less hacky (future proof) way of transforming union types to intersections.\r\n\u003c!-- A summary of what you'd like to see added or changed -->\r\n\r\n## Use Cases\r\n\r\nThe most common use case I can think of is a basic action system where each action has some kind of input data it can work with to determine its disabled status. An actionGroup which packs multiple actions into one updater function needs to request the right input data type which is compatible to **all** of its actions.\r\n\r\nWhile it's possible in the current version of typescript, it feels like a hack.\r\n\u003c!--\r\nWhat do you want to use this for?\r\nWhat shortcomings exist with current approaches?\r\n-->\r\n\r\n## Examples\r\n\r\nA more presentable form by describing a car dashboard:\r\n\r\n```ts\r\ninterface Gauge\u003cT> {\r\n display(data: T): void;\r\n}\r\n\r\nclass SpeedGauge implements Gauge\u003c{ speed: number; }> {\r\n display(data: { speed: number; }) {\r\n }\r\n}\r\n\r\nclass TempGauge implements Gauge\u003c{ temperature: number; }> {\r\n display(data: { temperature: number; }) {\r\n }\r\n}\r\n\r\nclass RevGauge implements Gauge\u003c{ rev: number; }> {\r\n display(data: { rev: number; }) {\r\n }\r\n}\r\n\r\ntype ExtractGaugeType\u003cT> = T extends Gauge\u003cinfer U> ? U : never;\r\n\r\n// evil magic by jcalz https://stackoverflow.com/a/50375286\r\n// I would like to future proof or have a better version of this\r\ntype UnionToIntersection\u003cU> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;\r\n\r\nclass Dashboard\u003cT extends Gauge\u003cunknown>> {\r\n constructor(public gauges: T[]) {\r\n }\r\n\r\n display(data: UnionToIntersection\u003cExtractGaugeType\u003cT>>) {\r\n this.gauges.forEach((g) => g.display(data));\r\n }\r\n}\r\n\r\nconst myDashboard = new Dashboard([new SpeedGauge(), new TempGauge(), new RevGauge()]);\r\n\r\n/*\r\nthe type is: { rev: number; } & { speed: number; } & { temperature: number; }\r\n*/\r\nmyDashboard.display({ // Ok\r\n speed: 50,\r\n rev: 2000,\r\n temperature: 85\r\n});\r\n\r\nmyDashboard.display({ // Error: property \"rev\" is missing\r\n speed: 50,\r\n temperature: 85\r\n});\r\n```\r\n\u003c!-- Show how this would be used and what the behavior would be -->\r\n\r\n## Checklist\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, 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","author":{"url":"https://github.com/zenorbi","@type":"Person","name":"zenorbi"},"datePublished":"2019-01-25T23:53:20Z","url":"https://github.com/microsoft/TypeScript/issues/29594"}
| 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:2042de8d-b00f-e5de-2b37-7e1845f2d332 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B1E0:2AAD2E:253FE6E:31BF5D6:6B01A425 |
| html-safe-nonce | 528b45ab8669bcdfc31b23aa5afcc7db15e696cd7f04516810a09c60dc0960b1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMUUwOjJBQUQyRToyNTNGRTZFOjMxQkY1RDY6NkIwMUE0MjUiLCJ2aXNpdG9yX2lkIjoiMzEyMDEwNjczOTQ2MzIwMTgyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 26aa508e8b13568ec6f24c20d935bb616ad988c7b26c11243e9718df40720b7e |
| hovercard-subject-tag | issue:403376334 |
| 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/29594/issue_layout |
| twitter:image | https://opengraph.githubassets.com/984f6edbafc529573b565a3e7d3e74b6a1973eeb4d3603764275c6b41a0af98d/microsoft/TypeScript/issues/29594 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/984f6edbafc529573b565a3e7d3e74b6a1973eeb4d3603764275c6b41a0af98d/microsoft/TypeScript/issues/29594 |
| og:image:alt | Search Terms union to intersection, type merge Suggestion I'd like to either standardize (meaning documented behavior) or have a less hacky (future proof) way of transforming union types to interse... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zenorbi |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width