Title: Incorrect narrowing of union to `never` after no-op `switch` in `while` loop · Issue #47539 · microsoft/TypeScript · GitHub
Open Graph Title: Incorrect narrowing of union to `never` after no-op `switch` in `while` loop · Issue #47539 · microsoft/TypeScript
X Title: Incorrect narrowing of union to `never` after no-op `switch` in `while` loop · Issue #47539 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms switch while union never 🕗 Version & Regression Information Playground link with relevant code 💻 Code declare function isNever(n: never): boolean; function f() { let foo: "aaa" | "bbb" = "aaa"; while (true) { sw...
Open Graph Description: Bug Report 🔎 Search Terms switch while union never 🕗 Version & Regression Information Playground link with relevant code 💻 Code declare function isNever(n: never): boolean; function f() { let foo: ...
X Description: Bug Report 🔎 Search Terms switch while union never 🕗 Version & Regression Information Playground link with relevant code 💻 Code declare function isNever(n: never): boolean; function f() { let f...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/47539
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect narrowing of union to `never` after no-op `switch` in `while` loop","articleBody":"# Bug Report\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 What search terms did you use when trying to find an existing bug report?\r\n List them here so people in the future can find this one more easily.\r\n-->\r\nswitch while union never\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n\u003c!-- When did you start seeing this bug occur?\r\n\r\n- This changed between versions 3.7 (good) and 3.8 (bad)\r\n\r\n### ⏯ Playground Link\r\n\r\n\u003c!--\r\n A link to a TypeScript Playground \"Share\" link which shows this behavior\r\n\r\n The TypeScript Workbench can be used for more complex setups, try\r\n https://www.typescriptlang.org/dev/bug-workbench/\r\n\r\n As a last resort, you can link to a repo, but these will be slower for us to investigate.\r\n-->\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.5.4#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXywGcA5EANxBgApUAueVcygSnoCMccIQpUBuAFACU6bHiRVm8AN4D48+NwxJO9AERRNa+AB94atoe0BefZqhrBC+AHcAFlm7wqGGMhBTZ164RtYMYHbOiJyect4RYFCECBpatOERAL5CEfJYiMGc8Ma5Zlphad4hODn6hmyWid5J8CAQMQSZVESkFNQlzJ7wAPQ9BOg4MHCY8ADyANLVRWxwUADWVmkp1ikpQA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\ndeclare function isNever(n: never): boolean;\r\n\r\nfunction f() {\r\n let foo: \"aaa\" | \"bbb\" = \"aaa\";\r\n while (true) {\r\n switch (foo) {\r\n case \"aaa\":\r\n }\r\n\r\n if (foo === \"aaa\") {\r\n foo = \"bbb\";\r\n } else if (isNever(foo)) { // incorrect OK\r\n break;\r\n }\r\n }\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe code incorrectly checks as OK.\r\n\r\n### 🙂 Expected behavior\r\n\r\nThe call to `isNever` should fail; on the second iteration of the loop `isNever` is invoked with `\"bbb\"`\r\n\r\nNote that we don't see this behavior with the equivalent `if`:\r\n```ts\r\ndeclare function isNever(n: never): boolean;\r\n\r\nfunction f() {\r\n let foo: \"aaa\" | \"bbb\" = \"aaa\";\r\n while (true) {\r\n //switch (foo) {\r\n // case \"aaa\":\r\n //}\r\n if (foo === \"aaa\") { }\r\n\r\n if (foo === \"aaa\") {\r\n foo = \"bbb\";\r\n } else if (isNever(foo)) { // correct error\r\n break;\r\n }\r\n }\r\n}\r\n```\r\n\r\nSee also #47538","author":{"url":"https://github.com/RyanCavanaugh","@type":"Person","name":"RyanCavanaugh"},"datePublished":"2022-01-21T03:57:37Z","url":"https://github.com/microsoft/TypeScript/issues/47539"}
| 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:d3bb80d7-16f2-8c7c-b9d0-13de89c02c6b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A76A:363D8E:2C0B0E1:37DC159:6B0198FA |
| html-safe-nonce | e0dfb3e11da2080b931c976542aa94990722db70800cc48482a616563b5a1209 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzZBOjM2M0Q4RToyQzBCMEUxOjM3REMxNTk6NkIwMTk4RkEiLCJ2aXNpdG9yX2lkIjoiNzU1MzQ1NDc1MDcwOTE2MDE4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f307ae64dbe93741f439a2a94b32dd849eb014fc192c40812522698055feb710 |
| hovercard-subject-tag | issue:1110024408 |
| 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/47539/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c822ff57b126af6069f4afe54382857c4271c14189288afec1c1f5dd7fb2a3bc/microsoft/TypeScript/issues/47539 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c822ff57b126af6069f4afe54382857c4271c14189288afec1c1f5dd7fb2a3bc/microsoft/TypeScript/issues/47539 |
| og:image:alt | Bug Report 🔎 Search Terms switch while union never 🕗 Version & Regression Information Playground link with relevant code 💻 Code declare function isNever(n: never): boolean; function f() { let foo: ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RyanCavanaugh |
| 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