René's URL Explorer Experiment


Title: `typeof ... === "undefined"` check on mapped object member doesn't narrow correctly, for only some purposes · Issue #52440 · microsoft/TypeScript · GitHub

Open Graph Title: `typeof ... === "undefined"` check on mapped object member doesn't narrow correctly, for only some purposes · Issue #52440 · microsoft/TypeScript

X Title: `typeof ... === "undefined"` check on mapped object member doesn't narrow correctly, for only some purposes · Issue #52440 · microsoft/TypeScript

Description: Bug Report 🔎 Search Terms undefined type guard narrowing fails 🕗 Version & Regression Information Between 4.2.3 and 4.3.5, the narrowing took effect on the second call to takesArray(), which previously reported a similar error. The error...

Open Graph Description: Bug Report 🔎 Search Terms undefined type guard narrowing fails 🕗 Version & Regression Information Between 4.2.3 and 4.3.5, the narrowing took effect on the second call to takesArray(), which previo...

X Description: Bug Report 🔎 Search Terms undefined type guard narrowing fails 🕗 Version & Regression Information Between 4.2.3 and 4.3.5, the narrowing took effect on the second call to takesArray(), which pr...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/52440

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.113.3


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`typeof ... === \"undefined\"` check on mapped object member doesn't narrow correctly, for only some purposes","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\nundefined type guard narrowing fails\r\n\r\n### 🕗 Version & Regression Information\r\n- Between 4.2.3 and 4.3.5, the narrowing took effect on the second call to takesArray(), which previously reported a similar error. \r\n- The error message changed between 4.7.4 and 4.8.4. \r\n- This appears related to #50340, which @a-tarasyuk may have fixed in #50344 (Aug. 31 ’22), but I’m still seeing the issue on the latest nightly (v5.0.0-dev.20230126) so I don't think it's a duplicate.  \r\n- I also don't think this is a duplicate of #51700 fixed in #51720 (Dec. 5 '22) as that focuses on the optional chaining operator.\r\n\r\n### ⏯ Playground Link\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBAsghmSATAKuaBeKBvA2iqASwDsoAKAcgEEAbMACzgqgB8oKAhCYJgSgF0AXDlwkkEAB7CAzsABOJAOb8ow4gFcAtgCMIc3PwC+AbkMBYAFDiAxjThzoAM3XFrwQgHtSPANYRpVHJycCBk9ooADGpauvr8vKpQ2h4eNBBwxMaWkmAecsBQ1l6yUOKaHgBipFjOru5eADwEksAQxEjSUH4gHo6wCMhokAB8ZJZQE10QIMIoADRQAPSLtvaEjiBKUMCMBTuEnQddxB4A7sSL0pDW64QQSOOTcmfCAAr27nA0DfCI90MQfD8YaWBLYR4TRx5MhpApwYDyQjadStIikZ6nMEQyYTIrEErwxHI1oANS+6kwUAxuEJCmJEH4WQsOJxy1pSJREDJNAphQ86hoSCS0Bc4kcJHuUHoeggCx28IonQA8gBpbE43z+QLBULs+ncim8YxLRZ6ORQOCdHIQNz3dWTZYAVWk0F623oR2kIGIPAkVIgAEd1IQHELtCApQiwNJBMt-DQSMAAHR5RSLJAeazSRZ2VqyRZyAX+RYnAC0oog4uIduZLImy2J7ugZrypQ8-mIFDhf3sUFO0tI6mkWwABuswgi6ZyDdAAIQYLAVqv3MEj+0TcegSBuvXT8lzhfsJcSpAULG1usauB+AJBEITol7nkQI0m3ek-dEToaHR6Azrlllj7QgdigY9qyFYh7AxSV+WABZG2rCQCgTaspUtKAW3NaQQiUADWUWCh3y5fdmCOXJpGHbQaAjChwPuCgk22aQyAARgADgiAAWdjeHwyYoTkGFuCgTQIF-c0d0nDkP2fc9LwUvl8VSCAkxoDxFEoFAPU6aCQjRWx1HEToKAAajEiTTMYs8mUUyZzAvFkHJxByHKAA)\r\n\r\n### 💻 Code\r\n```ts\r\ntype MappedType = {[T in ('Alpha' | 'Beta')]: {[index: string] : number[]};}\r\ndeclare function takesArray(arg0: number[]) : boolean;\r\nexport const demoFn = function\u003cT extends keyof MappedType>(\r\n    key: T, //clarifying that this is known/specified\r\n    row: Partial\u003cMappedType[T]>\r\n) {\r\n    for(let attribute in row) {\r\n        const attributeValue = row[attribute];\r\n        //attributeValue could be undefined here, that's OK\r\n        takesArray(attributeValue); //err as expected\r\n        //Use of this syntax required by https://eslint.org/docs/latest/rules/no-undefined\r\n        //but the error doesn't appear when using `if(attributeValue !== undefined) {`\r\n        if(typeof attributeValue !== 'undefined') {\r\n            takesArray(attributeValue); //attributeValue is number[]\r\n            // with undefined narrowed out, but next line has error saying\r\n            //'attributeValue' is possibly 'undefined'. ts(18048)\r\n            for(let member of attributeValue) {\r\n                console.log('This array includes '+member+'.');\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nError `'attributeValue' is possibly 'undefined'` (for the purposes of a for loop but not for the purposes of a function call) inside a type-guard conditional checking for undefined, where attributeValue is a constant originally copied from a mapped object type.  \r\n\r\n### 🙂 Expected behavior\r\n\r\n - Only the first error, on the line with the comment \"err as expected\".  \r\n - I would also expect TS to be consistent about whether `attributeValue` can be undefined within the two immediate children lines in the conditional (function call and for loop). \r\n - I would also expect TS to be consistent between the use of `typeof attributeValue !== 'undefined'` and `attributeValue !== undefined`.\r\n - I would also expect TS to behave consistently whether the same nominal type originally comes from a mapped type or not.  Note that the sample code above uses a mildly complex derived type, which is still **much** simpler than the inspiring example, and the motivation for this was left out of the example to get it closer to minimal with focus on the failing type guard.  The \"workaround\" of using simpler non-derived types would NOT solve the issue here. ","author":{"url":"https://github.com/wbt","@type":"Person","name":"wbt"},"datePublished":"2023-01-26T21:33:16Z","url":"https://github.com/microsoft/TypeScript/issues/52440"}

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:c3035d45-7003-1ec6-410d-7a53a7713e6e
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD62C:E08F0:5658CF:79CF27:6B04762C
html-safe-nonce521e8d9a9027e2fcbf939b80297e7770a7332e5553cd3a5bfe4f5444ee03f607
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENjJDOkUwOEYwOjU2NThDRjo3OUNGMjc6NkIwNDc2MkMiLCJ2aXNpdG9yX2lkIjoiNzY1NTY4Mzg1NDM0NjUxNjAxMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac5a90ab9a2e7e652bf1566a68bf2e3c3458cf262e64a91e847334ab259413fbc4
hovercard-subject-tagissue:1558808061
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/52440/issue_layout
twitter:imagehttps://opengraph.githubassets.com/508aacc48d8416aec606d855882861b27b7d83307bdd42da9599773b1d11cc99/microsoft/TypeScript/issues/52440
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/508aacc48d8416aec606d855882861b27b7d83307bdd42da9599773b1d11cc99/microsoft/TypeScript/issues/52440
og:image:altBug Report 🔎 Search Terms undefined type guard narrowing fails 🕗 Version & Regression Information Between 4.2.3 and 4.3.5, the narrowing took effect on the second call to takesArray(), which previo...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamewbt
hostnamegithub.com
expected-hostnamegithub.com
None9c099f1b85def81ca352464c276510dfef32deaf3c305404e65b8d270284a58b
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
releasecf73f57448d52b3a7b619195e182b33d3b3454ca
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/52440#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F52440
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%2F52440
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/52440
Reloadhttps://github.com/microsoft/TypeScript/issues/52440
Reloadhttps://github.com/microsoft/TypeScript/issues/52440
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests320 (320)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/52440
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/52440
#52456https://github.com/microsoft/TypeScript/pull/52456
typeof ... === "undefined" check on mapped object member doesn't narrow correctly, for only some purposeshttps://github.com/microsoft/TypeScript/issues/52440#top
#52456https://github.com/microsoft/TypeScript/pull/52456
BugA bug in TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Bug%22
Backloghttps://github.com/microsoft/TypeScript/milestone/29
https://github.com/wbt
wbthttps://github.com/wbt
on Jan 26, 2023https://github.com/microsoft/TypeScript/issues/52440#issue-1558808061
typeof ... === "undefined" check on discriminated union of undefined and object type doesn't narrow correctly #50340https://github.com/microsoft/TypeScript/issues/50340
Oleksandr Tarasiuk (@a-tarasyuk)https://github.com/a-tarasyuk
fix(50340): typeof ... === "undefined" check on discriminated union of undefined and object type doesn't narrow correctly #50344https://github.com/microsoft/TypeScript/pull/50344
Narrowing with typeof and optional chaining does not work specifically with a union containing boolean unless wrapped in parentheses #51700https://github.com/microsoft/TypeScript/issues/51700
Fix narrowing by typeof applied to discriminant property #51720https://github.com/microsoft/TypeScript/pull/51720
Playground link with relevant codehttps://www.typescriptlang.org/play#code/C4TwDgpgBAsghmSATAKuaBeKBvA2iqASwDsoAKAcgEEAbMACzgqgB8oKAhCYJgSgF0AXDlwkkEAB7CAzsABOJAOb8ow4gFcAtgCMIc3PwC+AbkMBYAFDiAxjThzoAM3XFrwQgHtSPANYRpVHJycCBk9ooADGpauvr8vKpQ2h4eNBBwxMaWkmAecsBQ1l6yUOKaHgBipFjOru5eADwEksAQxEjSUH4gHo6wCMhokAB8ZJZQE10QIMIoADRQAPSLtvaEjiBKUMCMBTuEnQddxB4A7sSL0pDW64QQSOOTcmfCAAr27nA0DfCI90MQfD8YaWBLYR4TRx5MhpApwYDyQjadStIikZ6nMEQyYTIrEErwxHI1oANS+6kwUAxuEJCmJEH4WQsOJxy1pSJREDJNAphQ86hoSCS0Bc4kcJHuUHoeggCx28IonQA8gBpbE43z+QLBULs+ncim8YxLRZ6ORQOCdHIQNz3dWTZYAVWk0F623oR2kIGIPAkVIgAEd1IQHELtCApQiwNJBMt-DQSMAAHR5RSLJAeazSRZ2VqyRZyAX+RYnAC0oog4uIduZLImy2J7ugZrypQ8-mIFDhf3sUFO0tI6mkWwABuswgi6ZyDdAAIQYLAVqv3MEj+0TcegSBuvXT8lzhfsJcSpAULG1usauB+AJBEITol7nkQI0m3ek-dEToaHR6Azrlllj7QgdigY9qyFYh7AxSV+WABZG2rCQCgTaspUtKAW3NaQQiUADWUWCh3y5fdmCOXJpGHbQaAjChwPuCgk22aQyAARgADgiAAWdjeHwyYoTkGFuCgTQIF-c0d0nDkP2fc9LwUvl8VSCAkxoDxFEoFAPU6aCQjRWx1HEToKAAajEiTTMYs8mUUyZzAvFkHJxByHKAA
wbthttps://github.com/wbt
on Jan 26, 2023https://github.com/microsoft/TypeScript/issues/52440#event-8368074152
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Jan 27, 2023https://github.com/microsoft/TypeScript/issues/52440#issuecomment-1405879697
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
BugA bug in TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Bug%22
Help Wantedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Help%20Wanted%22
on Jan 27, 2023https://github.com/microsoft/TypeScript/issues/52440#event-8369152881
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
Backloghttps://github.com/microsoft/TypeScript/milestone/29
on Jan 27, 2023https://github.com/microsoft/TypeScript/issues/52440#event-8369153061
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#52456https://github.com/microsoft/TypeScript/pull/52456
on Jan 30, 2023https://github.com/microsoft/TypeScript/issues/52440#event-8391243875
microsofthttps://github.com/microsoft
on Oct 22, 2025https://github.com/microsoft/TypeScript/issues/52440#event-20417657720
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/52440
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/52440
BugA bug in TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Bug%22
BacklogNo due datehttps://github.com/microsoft/TypeScript/milestone/29
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.