Title: type narrowing for instance fields should be discarded after await · Issue #31429 · microsoft/TypeScript · GitHub
Open Graph Title: type narrowing for instance fields should be discarded after await · Issue #31429 · microsoft/TypeScript
X Title: type narrowing for instance fields should be discarded after await · Issue #31429 · microsoft/TypeScript
Description: TypeScript Version: 3.4.1, 3.3.0 Search Terms: type narrow, instance field, discard, await Code class C { private b: boolean = false; async foo() { this.b = true; await new Promise(resolve => setTimeout(resolve, 1000)); /* This condition...
Open Graph Description: TypeScript Version: 3.4.1, 3.3.0 Search Terms: type narrow, instance field, discard, await Code class C { private b: boolean = false; async foo() { this.b = true; await new Promise(resolve => setTi...
X Description: TypeScript Version: 3.4.1, 3.3.0 Search Terms: type narrow, instance field, discard, await Code class C { private b: boolean = false; async foo() { this.b = true; await new Promise(resolve => se...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31429
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"type narrowing for instance fields should be discarded after await","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.1, 3.3.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:** type narrow, instance field, discard, await\r\n\r\n**Code**\r\n\r\n```ts\r\nclass C {\r\n private b: boolean = false;\r\n async foo() {\r\n this.b = true;\r\n await new Promise(resolve => setTimeout(resolve, 1000));\r\n /*\r\n This condition will always return 'false' since\r\n the types 'true' and 'false' have no overlap.\r\n -----\r\n (property) C.b: true\r\n */\r\n if (this.b == false) {\r\n //After 1s, this will be logged\r\n console.log(\"b was set to false\");\r\n }\r\n }\r\n\r\n setBToFalse() {\r\n this.b = false;\r\n }\r\n}\r\n\r\nconst c = new C();\r\n//This sets b to true, waits 1s, checks b\r\nc.foo();\r\n//We set b to false immediately\r\nc.setBToFalse();\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nComparison to `false` should work.\r\n\r\nType of `this.b` should be `boolean`.\r\n\r\n**Actual behavior:**\r\n\r\nComparison to `false` does not work.\r\n\r\nType of `this.b` is `true`.\r\n\r\nThis example is contrived but my actual use case is with `enum` type comparisons.\r\nI *know* the enum value can possibly change after the `await` but TS doesn't know that.\r\n\r\n**Workaround:**\r\n\r\nUse a type assertion,\r\n\r\n```ts\r\nif ((this.b as boolean) == false) {\r\n\r\n}\r\n```\r\n\r\n**Playground Link:** [Here](http://www.typescriptlang.org/play/#src=class%20C%20%7B%0D%0A%20%20%20%20private%20b%3A%20boolean%20%3D%20false%3B%0D%0A%20%20%20%20async%20foo()%20%7B%0D%0A%20%20%20%20%20%20%20%20this.b%20%3D%20true%3B%0D%0A%20%20%20%20%20%20%20%20await%20new%20Promise(resolve%20%3D%3E%20setTimeout(resolve%2C%201000))%3B%0D%0A%20%20%20%20%20%20%20%20%2F*%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20This%20condition%20will%20always%20return%20'false'%20since%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20the%20types%20'true'%20and%20'false'%20have%20no%20overlap.%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20-----%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20(property)%20C.b%3A%20true%0D%0A%20%20%20%20%20%20%20%20*%2F%0D%0A%20%20%20%20%20%20%20%20if%20(this.b%20%3D%3D%20false)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FAfter%201s%2C%20this%20will%20be%20logged%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log(%22b%20was%20set%20to%20false%22)%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20setBToFalse()%20%7B%0D%0A%20%20%20%20%20%20%20%20this.b%20%3D%20false%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20c%20%3D%20new%20C()%3B%0D%0A%2F%2FThis%20sets%20b%20to%20true%2C%20waits%201s%2C%20checks%20b%0D%0Ac.foo()%3B%0D%0A%2F%2FWe%20set%20b%20to%20false%20immediately%0D%0Ac.setBToFalse()%3B)\r\n","author":{"url":"https://github.com/AnyhowStep","@type":"Person","name":"AnyhowStep"},"datePublished":"2019-05-16T16:24:31Z","url":"https://github.com/microsoft/TypeScript/issues/31429"}
| 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:975fa9c8-762b-4e9c-9c76-b91f8a681781 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9BF2:1D1028:96D963:CBEF71:6B04DAC1 |
| html-safe-nonce | 1aa98053156a6c9b603b1d76aea4ab449ed22d26d4cb72de6cc2e4501035e971 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QkYyOjFEMTAyODo5NkQ5NjM6Q0JFRjcxOjZCMDREQUMxIiwidmlzaXRvcl9pZCI6IjQzNTk3MjM4NjczMTY0NTYzMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e5ab00fd52ca6a6c1cce0624fbf2fd7a36584866ae536c96795d257d828836d8 |
| hovercard-subject-tag | issue:445048650 |
| 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/31429/issue_layout |
| twitter:image | https://opengraph.githubassets.com/431125640f52be64be00b7346f63c81b916863de2ea95e740b07dd8f978b53c7/microsoft/TypeScript/issues/31429 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/431125640f52be64be00b7346f63c81b916863de2ea95e740b07dd8f978b53c7/microsoft/TypeScript/issues/31429 |
| og:image:alt | TypeScript Version: 3.4.1, 3.3.0 Search Terms: type narrow, instance field, discard, await Code class C { private b: boolean = false; async foo() { this.b = true; await new Promise(resolve => setTi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AnyhowStep |
| 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