Title: Type guards are not 'reset' after awaiting a promise · Issue #27900 · microsoft/TypeScript · GitHub
Open Graph Title: Type guards are not 'reset' after awaiting a promise · Issue #27900 · microsoft/TypeScript
X Title: Type guards are not 'reset' after awaiting a promise · Issue #27900 · microsoft/TypeScript
Description: TypeScript Version: 3.2.0-dev.20181011 (though I found it while using 3.1) Search Terms: async await type check change after await Code let elem: number | null = 5; async function main() { const p1 = test() const p2 = breaker() await Pro...
Open Graph Description: TypeScript Version: 3.2.0-dev.20181011 (though I found it while using 3.1) Search Terms: async await type check change after await Code let elem: number | null = 5; async function main() { const p1...
X Description: TypeScript Version: 3.2.0-dev.20181011 (though I found it while using 3.1) Search Terms: async await type check change after await Code let elem: number | null = 5; async function main() { const p1...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/27900
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type guards are not 'reset' after awaiting a promise","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.20181011 (though I found it while using 3.1)\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** async await type check change after await\r\n\r\n**Code**\r\n\r\n```ts\r\nlet elem: number | null = 5;\r\n\r\nasync function main() {\r\n const p1 = test()\r\n const p2 = breaker()\r\n await Promise.all([p1, p2])\r\n}\r\n\r\nasync function test() {\r\n if (typeof elem === \"number\") {\r\n // Type checks elem is number, type is number.\r\n console.log(elem.toString())\r\n await delay(500)\r\n // Type of elem is still number, even though it could be set to null (in breaker) \r\n console.log(elem.toString());\r\n }\r\n}\r\n\r\nfunction test2() {\r\n if (typeof elem === \"number\") {\r\n // Type of elem is number.\r\n console.log(elem.toString())\r\n delay(500).then(() => {\r\n // Type is now number | null, which is correct\r\n console.log(elem.toString())\r\n })\r\n }\r\n}\r\n\r\nasync function breaker() {\r\n await delay(250)\r\n elem = null;\r\n}\r\n\r\nasync function delay(ms: number) {\r\n return new Promise((resolve, reject) => setTimeout(resolve, ms))\r\n}\r\n\r\nmain()\r\n```\r\n\r\n**Expected behavior:**\r\n2 errors, one in test() after the await that elem is type number | null, and another in test2() in the callback of the delay promise that elem is type number | null\r\n\r\n**Actual behavior:**\r\nOnly the second error is showing up. Commenting out test2 will compile fine but fail at runtime with \"Cannot read property 'toString' of null\" error.\r\n\r\n**Playground Link:** [link](https://www.typescriptlang.org/play/#src=let%20elem%3A%20number%20%7C%20null%20%3D%205%3B%0D%0A%0D%0Aasync%20function%20main()%20%7B%0D%0A%20%20%20%20const%20p1%20%3D%20test()%0D%0A%20%20%20%20const%20p2%20%3D%20breaker()%0D%0A%20%20%20%20await%20Promise.all(%5Bp1%2C%20p2%5D)%0D%0A%7D%0D%0A%0D%0Aasync%20function%20test()%20%7B%0D%0A%20%20%20%20if%20(typeof%20elem%20%3D%3D%3D%20%22number%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20Type%20checks%20elem%20is%20number%2C%20type%20is%20number.%0D%0A%20%20%20%20%20%20%20%20console.log(elem.toString())%0D%0A%20%20%20%20%20%20%20%20await%20delay(500)%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20Type%20of%20elem%20is%20still%20number%2C%20even%20though%20it%20could%20be%20set%20to%20null%20(in%20breaker)%20%0D%0A%20%20%20%20%20%20%20%20console.log(elem.toString())%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Afunction%20test2()%20%7B%0D%0A%20%20%20%20if%20(typeof%20elem%20%3D%3D%3D%20%22number%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20Type%20of%20elem%20is%20number.%0D%0A%20%20%20%20%20%20%20%20console.log(elem.toString())%0D%0A%20%20%20%20%20%20%20%20delay(500).then(()%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Type%20is%20now%20number%20%7C%20null%2C%20which%20is%20correct%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log(elem.toString())%0D%0A%20%20%20%20%20%20%20%20%7D)%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Aasync%20function%20breaker()%20%7B%0D%0A%20%20%20%20await%20delay(250)%0D%0A%20%20%20%20elem%20%3D%20null%3B%0D%0A%7D%0D%0A%0D%0Aasync%20function%20delay(ms%3A%20number)%20%7B%0D%0A%20%20%20%20return%20new%20Promise((resolve%2C%20reject)%20%3D%3E%20setTimeout(resolve%2C%20ms))%0D%0A%7D%0D%0A%0D%0Amain()) (Will need to enable strictNullChecks)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n\r\nIn short, the issue seems to be that after an await, any type narrowing checks that occurred before are still in effect, which can cause problems if the variable changed to something else (in this case, set to null) before the await finishes but after the check itself.\r\n","author":{"url":"https://github.com/remag9330","@type":"Person","name":"remag9330"},"datePublished":"2018-10-15T02:32:33Z","url":"https://github.com/microsoft/TypeScript/issues/27900"}
| 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:09a98853-ac4f-67b0-d263-ba393c7355a6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF6C:F7BE7:1C6DB5:25F00F:6B04E5F8 |
| html-safe-nonce | 261d0d433b1c78fb813a1d94dc12b3ddeeeded40372053e3e6d79afd7d2409af |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjZDOkY3QkU3OjFDNkRCNToyNUYwMEY6NkIwNEU1RjgiLCJ2aXNpdG9yX2lkIjoiNzEzMjkxNzk0MTM2NDEyMzEyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1a26cd2c06122c575ecaaa512611de22dfceea11c752ef83d3050c0c9486c5c4 |
| hovercard-subject-tag | issue:369976320 |
| 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/27900/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a56ce035036c3624339e1e3246fb775f8f259e236846d1b3272a045b04abf20a/microsoft/TypeScript/issues/27900 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a56ce035036c3624339e1e3246fb775f8f259e236846d1b3272a045b04abf20a/microsoft/TypeScript/issues/27900 |
| og:image:alt | TypeScript Version: 3.2.0-dev.20181011 (though I found it while using 3.1) Search Terms: async await type check change after await Code let elem: number | null = 5; async function main() { const p1... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | remag9330 |
| 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