Title: Switch over discriminated union inside while loop is broken · Issue #27493 · microsoft/TypeScript · GitHub
Open Graph Title: Switch over discriminated union inside while loop is broken · Issue #27493 · microsoft/TypeScript
X Title: Switch over discriminated union inside while loop is broken · Issue #27493 · microsoft/TypeScript
Description: TypeScript Version: 3.2.0-dev.20181002 Search Terms: tagged/discriminated unions unreachable/conditions Code enum Types { Str = 1, Num = 2 } type Instance = StrType | NumType; interface StrType { type: Types.Str; value: string; length: n...
Open Graph Description: TypeScript Version: 3.2.0-dev.20181002 Search Terms: tagged/discriminated unions unreachable/conditions Code enum Types { Str = 1, Num = 2 } type Instance = StrType | NumType; interface StrType { t...
X Description: TypeScript Version: 3.2.0-dev.20181002 Search Terms: tagged/discriminated unions unreachable/conditions Code enum Types { Str = 1, Num = 2 } type Instance = StrType | NumType; interface StrType { t...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/27493
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Switch over discriminated union inside while loop is broken","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.0-dev.20181002\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** tagged/discriminated unions unreachable/conditions \r\n\r\n**Code**\r\n\r\n```ts\r\nenum Types { Str = 1, Num = 2 }\r\ntype Instance = StrType | NumType;\r\ninterface StrType {\r\n type: Types.Str;\r\n value: string;\r\n length: number;\r\n}\r\ninterface NumType {\r\n type: Types.Num;\r\n value: number;\r\n}\r\n\r\nfunction func(inst: Instance) {\r\n if (false) {\r\n switch (inst.type) {\r\n case Types.Str: {\r\n console.log(inst.value.length);\r\n break;\r\n }\r\n case Types.Num: {\r\n console.log(inst.value.toExponential);\r\n break;\r\n }\r\n }\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nNo errors\r\n\r\n**Actual behavior:**\r\n```\r\nError:(17, 40) TS2339: Property 'length' does not exist on type 'string | number'.\r\n Property 'length' does not exist on type 'number'.\r\nError:(21, 40) TS2339: Property 'toExponential' does not exist on type 'string | number'.\r\n Property 'toExponential' does not exist on type 'string'.\r\n```\r\n\r\n**Code**\r\n```ts\r\nfunction func2(inst: Instance) {\r\n while (true) {\r\n switch (inst.type) {\r\n case Types.Str: {\r\n console.log(inst.value.length);\r\n break;\r\n }\r\n case Types.Num: {\r\n console.log(inst.value.toExponential);\r\n break;\r\n }\r\n }\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nNo errors\r\n\r\n**Actual behavior:**\r\n```\r\nError:(21, 40) TS2339: Property 'toExponential' does not exist on type 'string'.\r\n```\r\n\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\n[Playground](https://www.typescriptlang.org/play/index.html#src=enum%20Types%20%7B%20Str%20%3D%201%2C%20Num%20%3D%202%20%7D%0D%0Atype%20Instance%20%3D%20StrType%20%7C%20NumType%3B%0D%0Ainterface%20StrType%20%7B%0D%0A%20%20%20%20type%3A%20Types.Str%3B%0D%0A%20%20%20%20value%3A%20string%3B%0D%0A%20%20%20%20length%3A%20number%3B%0D%0A%7D%0D%0Ainterface%20NumType%20%7B%0D%0A%20%20%20%20type%3A%20Types.Num%3B%0D%0A%20%20%20%20value%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Afunction%20func(inst%3A%20Instance)%20%7B%0D%0A%20%20%20%20if%20(false)%20%7B%0D%0A%20%20%20%20%20%20%20%20switch%20(inst.type)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20case%20Types.Str%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20console.log(inst.value.length)%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20case%20Types.Num%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20console.log(inst.value.toExponential)%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%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%20func2(inst%3A%20Instance)%20%7B%0D%0A%20%20%20%20while%20(true)%20%7B%0D%0A%20%20%20%20%20%20%20%20switch%20(inst.type)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20case%20Types.Str%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20console.log(inst.value.length)%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20case%20Types.Num%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20console.log(inst.value.toExponential)%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\nNot found","author":{"url":"https://github.com/WiseBird","@type":"Person","name":"WiseBird"},"datePublished":"2018-10-02T07:42:06Z","url":"https://github.com/microsoft/TypeScript/issues/27493"}
| 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:5495de99-a786-518a-8aae-84a2257ba6cb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E92E:3EF53B:C6CAC7:10400EE:6B04DFF3 |
| html-safe-nonce | 59c3bf1828f7c6e06aa0ce478abbae25ca3b63303ce9e47fe891b33af61e55c2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTJFOjNFRjUzQjpDNkNBQzc6MTA0MDBFRTo2QjA0REZGMyIsInZpc2l0b3JfaWQiOiI1Njc4ODA2Njk2ODY5NTUyMTE1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1de6f55c80ae276cb95eaa0f3ecfa507b370f2e0cb251d9f30d676f82dd2800c |
| hovercard-subject-tag | issue:365776581 |
| 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/27493/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e268009585b5767b053d3c78d30393582303ab19a198d4d95cee6575a7f8a78d/microsoft/TypeScript/issues/27493 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e268009585b5767b053d3c78d30393582303ab19a198d4d95cee6575a7f8a78d/microsoft/TypeScript/issues/27493 |
| og:image:alt | TypeScript Version: 3.2.0-dev.20181002 Search Terms: tagged/discriminated unions unreachable/conditions Code enum Types { Str = 1, Num = 2 } type Instance = StrType | NumType; interface StrType { t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | WiseBird |
| hostname | github.com |
| expected-hostname | github.com |
| None | 970f03f35f02bb123d2b6f1884601d89a570dc02e5ef9612b1b6ca5c033b1796 |
| 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 | ebcec4199c8521b643c068ef118a1ba9a43b678a |
| 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