Title: Switch narrowing regression with nested unions · Issue #29012 · microsoft/TypeScript · GitHub
Open Graph Title: Switch narrowing regression with nested unions · Issue #29012 · microsoft/TypeScript
X Title: Switch narrowing regression with nested unions · Issue #29012 · microsoft/TypeScript
Description: TypeScript Version: 3.2.2, 3.3.0-dev.201xxxxx Search Terms: 3.2 switch union narrowing regression Code type Additive = '+' | '-'; type Multiplicative = '*' | '/'; interface AdditiveObj { key: Additive } interface MultiplicativeObj { key:...
Open Graph Description: TypeScript Version: 3.2.2, 3.3.0-dev.201xxxxx Search Terms: 3.2 switch union narrowing regression Code type Additive = '+' | '-'; type Multiplicative = '*' | '/'; interface AdditiveObj { key: Addit...
X Description: TypeScript Version: 3.2.2, 3.3.0-dev.201xxxxx Search Terms: 3.2 switch union narrowing regression Code type Additive = '+' | '-'; type Multiplicative = '*' | '/'; in...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/29012
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Switch narrowing regression with nested unions","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. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.2.2, 3.3.0-dev.201xxxxx\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** 3.2 switch union narrowing regression\r\n\r\n**Code**\r\n\r\n```ts\r\ntype Additive = '+' | '-';\r\ntype Multiplicative = '*' | '/';\r\n\r\ninterface AdditiveObj {\r\n key: Additive\r\n}\r\n\r\ninterface MultiplicativeObj {\r\n key: Multiplicative\r\n}\r\n\r\ntype Obj = AdditiveObj | MultiplicativeObj\r\n\r\nexport function foo(obj: Obj) {\r\n switch (obj.key) {\r\n case '+': {\r\n onlyPlus(obj.key);\r\n return;\r\n }\r\n }\r\n}\r\n\r\nfunction onlyPlus(arg: '+') {\r\n return arg;\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n`obj.key` is narrowed to `'+'` in the switch and the call to `onlyPlus(obj.key)` type checks successfully.\r\n\r\n**Actual behavior:**\r\n`obj.key` is narrowed to `Additive` in the switch and the call to `onlyPlus(obj.key)` fails to type check\r\n\r\n**Playground Link:** http://www.typescriptlang.org/play/#src=type%20Additive%20%3D%20'%2B'%20%7C%20'-'%3B%0D%0Atype%20Multiplicative%20%3D%20'*'%20%7C%20'%2F'%3B%0D%0A%0D%0Ainterface%20AdditiveObj%20%7B%0D%0A%20%20%20%20key%3A%20Additive%0D%0A%7D%0D%0A%0D%0Ainterface%20MultiplicativeObj%20%7B%0D%0A%20%20%20%20key%3A%20Multiplicative%0D%0A%7D%0D%0A%0D%0Atype%20Obj%20%3D%20AdditiveObj%20%7C%20MultiplicativeObj%0D%0A%0D%0Aexport%20function%20foo(obj%3A%20Obj)%20%7B%0D%0A%20%20%20%20switch%20(obj.key)%20%7B%0D%0A%20%20%20%20%20%20%20%20case%20'%2B'%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20onlyPlus(obj.key)%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Afunction%20onlyPlus(arg%3A%20'%2B')%20%7B%0D%0A%20%20return%20arg%3B%0D%0A%7D%0D%0A\r\n\r\n**Related Issues:**\r\n* https://github.com/Microsoft/TypeScript/issues/28568\r\n* https://github.com/Microsoft/TypeScript/pull/27612\r\n* https://github.com/Microsoft/TypeScript/pull/27680\r\n\r\n**Note:**\r\nThis seems to be a regression between TS 3.1 and TS 3.2\r\n","author":{"url":"https://github.com/jwmerrill","@type":"Person","name":"jwmerrill"},"datePublished":"2018-12-13T23:07:45Z","url":"https://github.com/microsoft/TypeScript/issues/29012"}
| 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:2c89bd61-bf53-db1c-ec09-6e557969bed5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BF9C:163142:A3A516:E55501:6B048BFD |
| html-safe-nonce | 2dc653abada42df5b4a27a57366fed8c537b9cb69226028a61e5cec2826efacd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRjlDOjE2MzE0MjpBM0E1MTY6RTU1NTAxOjZCMDQ4QkZEIiwidmlzaXRvcl9pZCI6IjE1NDU3NzMxMzY5NDYxNzA4NzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8be03afd8df14e32180f9cce3c07a2b49ce9262a38e2d6384432f8cd3f6860c0 |
| hovercard-subject-tag | issue:390899453 |
| 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/29012/issue_layout |
| twitter:image | https://opengraph.githubassets.com/55479053b41a07949c4086e82755007f3e5d369026fccb1152abe2222cbf7689/microsoft/TypeScript/issues/29012 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/55479053b41a07949c4086e82755007f3e5d369026fccb1152abe2222cbf7689/microsoft/TypeScript/issues/29012 |
| og:image:alt | TypeScript Version: 3.2.2, 3.3.0-dev.201xxxxx Search Terms: 3.2 switch union narrowing regression Code type Additive = '+' | '-'; type Multiplicative = '*' | '/'; interface AdditiveObj { key: Addit... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jwmerrill |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9b297275a74a2c9a35898175c9477837c635d4f579277ebff3b2d6ebd012e247 |
| 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 | b51ce57ca6d4565a42856fc1c9897077c794597f |
| 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