René's URL Explorer Experiment


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

direct link

Domain: github.com

IPv4 address: 140.82.112.4


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:09a98853-ac4f-67b0-d263-ba393c7355a6
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idDF6C:F7BE7:1C6DB5:25F00F:6B04E5F8
html-safe-nonce261d0d433b1c78fb813a1d94dc12b3ddeeeded40372053e3e6d79afd7d2409af
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjZDOkY3QkU3OjFDNkRCNToyNUYwMEY6NkIwNEU1RjgiLCJ2aXNpdG9yX2lkIjoiNzEzMjkxNzk0MTM2NDEyMzEyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac1a26cd2c06122c575ecaaa512611de22dfceea11c752ef83d3050c0c9486c5c4
hovercard-subject-tagissue:369976320
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/27900/issue_layout
twitter:imagehttps://opengraph.githubassets.com/a56ce035036c3624339e1e3246fb775f8f259e236846d1b3272a045b04abf20a/microsoft/TypeScript/issues/27900
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/a56ce035036c3624339e1e3246fb775f8f259e236846d1b3272a045b04abf20a/microsoft/TypeScript/issues/27900
og:image:altTypeScript 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameremag9330
hostnamegithub.com
expected-hostnamegithub.com
None970f03f35f02bb123d2b6f1884601d89a570dc02e5ef9612b1b6ca5c033b1796
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseebcec4199c8521b643c068ef118a1ba9a43b678a
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/27900#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F27900
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F27900
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/27900
Reloadhttps://github.com/microsoft/TypeScript/issues/27900
Reloadhttps://github.com/microsoft/TypeScript/issues/27900
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests319 (319)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/27900
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/27900
Type guards are not 'reset' after awaiting a promisehttps://github.com/microsoft/TypeScript/issues/27900#top
Design Limitationhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Design%20Limitation%22
https://github.com/remag9330
remag9330https://github.com/remag9330
on Oct 15, 2018https://github.com/microsoft/TypeScript/issues/27900#issue-369976320
linkhttps://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()
https://github.com/nmain
Nicholai Main (nmain)https://github.com/nmain
on Oct 15, 2018https://github.com/microsoft/TypeScript/issues/27900#issuecomment-429956609
#9998https://github.com/microsoft/TypeScript/issues/9998
https://github.com/remag9330
remag9330https://github.com/remag9330
on Oct 15, 2018https://github.com/microsoft/TypeScript/issues/27900#issuecomment-430011790
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
Design Limitationhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Design%20Limitation%22
on Oct 16, 2018https://github.com/microsoft/TypeScript/issues/27900#event-1905756957
https://github.com/DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
on Oct 16, 2018https://github.com/microsoft/TypeScript/issues/27900#issuecomment-430098517
#9998https://github.com/microsoft/TypeScript/issues/9998
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Oct 17, 2018https://github.com/microsoft/TypeScript/issues/27900#event-1908255977
https://github.com/matthias-ccri
Matthias (matthias-ccri)https://github.com/matthias-ccri
on Feb 6, 2019https://github.com/microsoft/TypeScript/issues/27900#issuecomment-461214640
Playground linkhttps://www.typescriptlang.org/play/#src=%2F%2F%20An%20%22outside%22%20variable%20can%20be%20either%20undefined%20or%20an%20object.%0D%0Alet%20outsideVar%3A%20undefined%20%7C%20%7Ba%3A%20%7Bb%3A%20%7Bc%3A%20any%7D%7D%7D%3B%0D%0A%0D%0Aasync%20function%20main%20()%20%7B%0D%0A%20%20%20%20outsideVar%20%3D%20%7Ba%3A%20%7B%20b%3A%20%7B%20c%3A%20123%20%7D%7D%7D%3B%0D%0A%20%20%20%20await%20new%20Promise((resolve)%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20setTimeout(resolve%2C%20100)%3B%0D%0A%20%20%20%20%7D)%3B%0D%0A%20%20%20%20%2F%2F%20in%20the%20meantime%2C%20outsideVar%20gets%20unset%20but%20Typescript%20thinks%20it's%20still%20set.%0D%0A%20%20%20%20console.log(JSON.stringify(outsideVar))%3B%0D%0A%20%20%20%20console.log(outsideVar.a.b.c)%3B%0D%0A%20%20%20%20%2F%2F%20At%20runtime%2C%20outsideVar%20is%20undefined%2C%20and%20%0D%0A%20%20%20%20%2F%2F%20property%20access%20throws%20%22TypeError%3A%20Cannot%20read%20property%20'a'%20of%20undefined%22%0D%0A%7D%0D%0A%0D%0Amain()%3B%0D%0AoutsideVar%20%3D%20undefined%3B%20%2F%2F%20This%20will%20run%20before%20the%20%60await%60%20continues.%0D%0A%0D%0A%2F%2F%20Further%20reading%3A%0D%0A%2F%2F%20%20%20https%3A%2F%2Fgithub.com%2FMicrosoft%2FTypeScript%2Fissues%2F27900%0D%0A%2F%2F%20%20%20https%3A%2F%2Fgithub.com%2FMicrosoft%2FTypeScript%2Fissues%2F9998%0D%0A%0D%0A
https://github.com/thw0rted
James Bromwell (thw0rted)https://github.com/thw0rted
on Jun 23, 2020https://github.com/microsoft/TypeScript/issues/27900#issuecomment-648250751
#9998https://github.com/microsoft/TypeScript/issues/9998
Daniel Rosenwasser (@DanielRosenwasser)https://github.com/DanielRosenwasser
#9998https://github.com/microsoft/TypeScript/issues/9998
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/27900
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/27900
Design Limitationhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Design%20Limitation%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.