Title: proposal: Narrow Discriminated Unions by discarding types with no possible values · Issue #13300 · microsoft/TypeScript · GitHub
Open Graph Title: proposal: Narrow Discriminated Unions by discarding types with no possible values · Issue #13300 · microsoft/TypeScript
X Title: proposal: Narrow Discriminated Unions by discarding types with no possible values · Issue #13300 · microsoft/TypeScript
Description: It will be nice if TS narrowed Discriminated Unions by discarding types with no possible values (e.g. {tag: 'a' & 'b', x : 1}). Example: type A = {tag: 'a', x: 1} type B = {tag: 'b', y: 1} type TaggedAsB = {tag: 'b'} type T = (B | A) & T...
Open Graph Description: It will be nice if TS narrowed Discriminated Unions by discarding types with no possible values (e.g. {tag: 'a' & 'b', x : 1}). Example: type A = {tag: 'a', x: 1} type B = {tag: 'b', y: 1} type Tag...
X Description: It will be nice if TS narrowed Discriminated Unions by discarding types with no possible values (e.g. {tag: 'a' & 'b', x : 1}). Example: type A = {tag: 'a', x: 1} type B...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/13300
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"proposal: Narrow Discriminated Unions by discarding types with no possible values","articleBody":"It will be nice if TS narrowed Discriminated Unions by discarding types with no possible values (e.g. `{tag: 'a' & 'b', x : 1}`).\r\n\r\nExample:\r\n\r\n```ts\r\ntype A = {tag: 'a', x: 1} \r\ntype B = {tag: 'b', y: 1}\r\ntype TaggedAsB = {tag: 'b'}\r\n\r\ntype T = (B | A) & TaggedAsB // {tag: 'a' & 'b', x: 1} | {tag: 'b', y: 1}\r\n\r\nlet o : T\r\no.y // make legal, because the only possible values have type {tag: 'b', y: 1} \r\n```\r\nUse case:\r\n\r\n```ts\r\ntype Event = ({eventName : \"EVENT_A\", dataA : any} | \r\n {eventName : \"EVENT_B\", dataB : any})\r\n\r\n// define a callback for an specific event, with this new feature you can use a tag\r\n// and the intersection type operator to select an specific case from the Event DU\r\nfunction handlerEventA(event : Event & {eventName: \"EVENT_A\"}) {\r\n // with this new feature, here we don't need a runtime typeguard, \r\n // TS already knows what specific DU case is event:\r\n let x = event.dataA \r\n}\r\n```\r\n\r\nThis comes from the discussion at https://github.com/Microsoft/TypeScript/issues/13203","author":{"url":"https://github.com/nahuel","@type":"Person","name":"nahuel"},"datePublished":"2017-01-05T19:55:20Z","url":"https://github.com/microsoft/TypeScript/issues/13300"}
| 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:d1264f01-c0ed-29b6-6866-d7d079fc1ff3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E6FE:1204FD:7B81C9:A2D57A:6B04CAE1 |
| html-safe-nonce | 8d8f0667d71abf19fe62e3ae4922bb9fb2324fd413c84dece07e306275f55f73 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNkZFOjEyMDRGRDo3QjgxQzk6QTJENTdBOjZCMDRDQUUxIiwidmlzaXRvcl9pZCI6IjUzMTA5OTI1MDUxNjMyNzA4ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d9175fb1d0c48eb0909fff20f0b7a8996a5f3f3b7062a7b1ebcf46632537be50 |
| hovercard-subject-tag | issue:199044277 |
| 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/13300/issue_layout |
| twitter:image | https://opengraph.githubassets.com/de7c7532375862d2dadb99a4160f3b92c2b3dcdb60a56f30d0b4ade06311ea81/microsoft/TypeScript/issues/13300 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/de7c7532375862d2dadb99a4160f3b92c2b3dcdb60a56f30d0b4ade06311ea81/microsoft/TypeScript/issues/13300 |
| og:image:alt | It will be nice if TS narrowed Discriminated Unions by discarding types with no possible values (e.g. {tag: 'a' & 'b', x : 1}). Example: type A = {tag: 'a', x: 1} type B = {tag: 'b', y: 1} type Tag... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nahuel |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27ed421e5c2dac040a37529b5b9191edcc4a3de70521471e81fb4b4d9dc2aa7d |
| 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 | 63dc19014c621c67306d7899c04c78dcd0d95803 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width