Title: Variable incorrectly inferred across await boundary · Issue #31009 · microsoft/TypeScript · GitHub
Open Graph Title: Variable incorrectly inferred across await boundary · Issue #31009 · microsoft/TypeScript
X Title: Variable incorrectly inferred across await boundary · Issue #31009 · microsoft/TypeScript
Description: TypeScript Version: 3.4.0 Search Terms: enumeration async await Code function wait(tm: number) { return new Promise((resolve, reject) => { setTimeout(() => resolve(true), tm); }); } let x: "foo" | "bar"; async function baz() { x = "bar";...
Open Graph Description: TypeScript Version: 3.4.0 Search Terms: enumeration async await Code function wait(tm: number) { return new Promise((resolve, reject) => { setTimeout(() => resolve(true), tm); }); } let x: "foo" | ...
X Description: TypeScript Version: 3.4.0 Search Terms: enumeration async await Code function wait(tm: number) { return new Promise((resolve, reject) => { setTimeout(() => resolve(true), tm); }); } let x: &q...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31009
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Variable incorrectly inferred across await boundary","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.4.0\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** enumeration async await\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction wait(tm: number) {\r\n return new Promise((resolve, reject) => {\r\n setTimeout(() => resolve(true), tm);\r\n });\r\n}\r\n\r\nlet x: \"foo\" | \"bar\";\r\n\r\nasync function baz() {\r\n x = \"bar\";\r\n await wait(1000);\r\n if (x == \"foo\") {\r\n // erroneous: This condition will always return 'false' since the types '\"bar\"' and '\"foo\"' have no overlap.\r\n console.log(\"could be reached\");\r\n }\r\n}\r\n\r\nsetTimeout(() => x = \"foo\", 500);\r\nbaz();\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nCode should compile fine\r\n\r\n**Actual behavior:**\r\n\r\nCompiler believes x must be \"bar\" since it was assigned such at the start of the method. However since there is an await this is not true.\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\n\r\n[Playground link](https://www.typescriptlang.org/play/#src=function%20wait(tm%3A%20number)%20%7B%0D%0A%20%20%20return%20new%20Promise((resolve%2C%20reject)%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20setTimeout(()%20%3D%3E%20resolve(true)%2C%20tm)%3B%0D%0A%20%20%20%7D)%3B%0D%0A%7D%0D%0A%0D%0Alet%20x%3A%20%22foo%22%20%7C%20%22bar%22%3B%0D%0A%0D%0Aasync%20function%20baz()%20%7B%0D%0A%20%20%20%20x%20%3D%20%22bar%22%3B%0D%0A%20%20%20%20await%20wait(1000)%3B%0D%0A%20%20%20%20if%20(x%20%3D%3D%20%22foo%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20erroneous%3A%20This%20condition%20will%20always%20return%20'false'%20since%20the%20types%20'%22bar%22'%20and%20'%22foo%22'%20have%20no%20overlap.%0D%0A%20%20%20%20%20%20%20%20console.log(%22could%20be%20reached%22)%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0AsetTimeout(()%20%3D%3E%20x%20%3D%20%22foo%22%2C%20500)%3B%0D%0Abaz()%3B)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n","author":{"url":"https://github.com/sceutre","@type":"Person","name":"sceutre"},"datePublished":"2019-04-18T08:02:04Z","url":"https://github.com/microsoft/TypeScript/issues/31009"}
| 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:4f126900-18fa-eb4a-8f49-7653aaf41e45 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BB4E:143847:A810B4:E3C97E:6B04DAD6 |
| html-safe-nonce | 18a152cb9ee9c270d8402db78449a1b55daec8b1c239bc3952941197a2e841d9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQjRFOjE0Mzg0NzpBODEwQjQ6RTNDOTdFOjZCMDREQUQ2IiwidmlzaXRvcl9pZCI6IjYzODk0NTA2Mjg0NTYwNDUyNzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3c8366043f0290169a7f63c789548d7597c01847576804214f676e305f50a5d3 |
| hovercard-subject-tag | issue:434640065 |
| 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/31009/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a2dca6774b7974f3826171bc97eaaf49f1aa7bda4e3da359f4cd902a533f45c1/microsoft/TypeScript/issues/31009 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a2dca6774b7974f3826171bc97eaaf49f1aa7bda4e3da359f4cd902a533f45c1/microsoft/TypeScript/issues/31009 |
| og:image:alt | TypeScript Version: 3.4.0 Search Terms: enumeration async await Code function wait(tm: number) { return new Promise((resolve, reject) => { setTimeout(() => resolve(true), tm); }); } let x: "foo" | ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sceutre |
| hostname | github.com |
| expected-hostname | github.com |
| None | 194dacb8acfd3e4d3ffb2704e0b1f10327f0dad690a7142acc7b0e0a2d073965 |
| 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 | 6c35540a0a2991ee20bd053ba2601cce33f72cb1 |
| 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