Title: Discriminated union not correctly narrowed by intersection · Issue #36323 · microsoft/TypeScript · GitHub
Open Graph Title: Discriminated union not correctly narrowed by intersection · Issue #36323 · microsoft/TypeScript
X Title: Discriminated union not correctly narrowed by intersection · Issue #36323 · microsoft/TypeScript
Description: I am filing this issue to re-open the discussion from #13300 and #18210 which are now locked. While the former was closed as a duplicate of the latter, which itself was closed in #18438, this change did not actually fix the original issu...
Open Graph Description: I am filing this issue to re-open the discussion from #13300 and #18210 which are now locked. While the former was closed as a duplicate of the latter, which itself was closed in #18438, this chang...
X Description: I am filing this issue to re-open the discussion from #13300 and #18210 which are now locked. While the former was closed as a duplicate of the latter, which itself was closed in #18438, this chang...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/36323
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Discriminated union not correctly narrowed by intersection","articleBody":"I am filing this issue to re-open the discussion from #13300 and #18210 which are now locked. While the former was closed as a duplicate of the latter, which itself was closed in #18438, this change did not actually fix the original issue in #13300. Here is an example:\r\n\r\n```ts\r\ntype A = { key: \"one\", value: number } | { key: \"two\", value: string };\r\n\r\ntype A_one = A & { key: \"one\" };\r\n\r\nfunction foo(a1: A_one): number {\r\n return a1.value; // error\r\n}\r\n```\r\n\r\n`A_one` should be typed as `{ key: \"one\", value: number }`, because there are no other possibilities, but is actually typed as `({ key: \"one\", value: number } & { key: \"one\" }) | ({ key: \"two\", value: string } & { key: \"one\" })`.\r\n\r\n----\r\n\r\nMotivation: https://stackoverflow.com/q/59835356/23649; code I was trying to write to solve this problem:\r\n\r\n```ts\r\nfunction foo\u003cK extends UnionA[\"key\"]>(val: UnionA & { key: K }): (UnionB & { key: K }) | undefined {\r\n const k: K = val.key; // not strictly necessary, but just to help reduce complexity below\r\n for (let i = 0; i \u003c b.length; i++) {\r\n if (b[i].key === k) {\r\n return b[i];\r\n }\r\n }\r\n return undefined;\r\n}\r\n```\r\n\r\nError message on the `return` line:\r\n```\r\nType 'UnionB' is not assignable to type '({ key: \"one\"; value: boolean; } & { key: K; }) | ({ key: \"two\"; value: string; } & { key: K; }) | undefined'.\r\n Type '{ key: \"one\"; value: boolean; }' is not assignable to type '({ key: \"one\"; value: boolean; } & { key: K; }) | ({ key: \"two\"; value: string; } & { key: K; }) | undefined'.\r\n Type '{ key: \"one\"; value: boolean; }' is not assignable to type '{ key: \"one\"; value: boolean; } & { key: K; }'.\r\n Type '{ key: \"one\"; value: boolean; }' is not assignable to type '{ key: K; }'.\r\n Types of property 'key' are incompatible.\r\n Type '\"one\"' is not assignable to type 'K'.\r\n '\"one\"' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint '\"one\" | \"two\"'.(2322)\r\n```","author":{"url":"https://github.com/jtbandes","@type":"Person","name":"jtbandes"},"datePublished":"2020-01-21T07:26:55Z","url":"https://github.com/microsoft/TypeScript/issues/36323"}
| 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:9d1fb617-3659-ef32-acf3-4195b78bf5d5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E288:38F238:1039BA:15F57E:6B04B15B |
| html-safe-nonce | e0150898d135e45cf8eb33eb445c8758680af93b681478a8b2314c361ac61fcf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMjg4OjM4RjIzODoxMDM5QkE6MTVGNTdFOjZCMDRCMTVCIiwidmlzaXRvcl9pZCI6IjEyMDAxNDY3Mzc3MDExMDYwMTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4313ffb454d77f94dd5c42343bddd33252f12cd802a417a1994b329a9540be85 |
| hovercard-subject-tag | issue:552672024 |
| 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/36323/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9a101f26866af213e3c8f75f5c6939932626a82542f3e6bef1516348947348d1/microsoft/TypeScript/issues/36323 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9a101f26866af213e3c8f75f5c6939932626a82542f3e6bef1516348947348d1/microsoft/TypeScript/issues/36323 |
| og:image:alt | I am filing this issue to re-open the discussion from #13300 and #18210 which are now locked. While the former was closed as a duplicate of the latter, which itself was closed in #18438, this chang... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jtbandes |
| hostname | github.com |
| expected-hostname | github.com |
| None | f1d110ea058a0915c1d8aa3fc2069fa15801e128f591e5762525abe77490ecb1 |
| 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 | ffa22a65e4c46d376940fe34b05e6a7e9c7fdb9b |
| 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