René's URL Explorer Experiment


Title: Null-Check is not fully recognized for `Partial` generic mapped types · Issue #42537 · microsoft/TypeScript · GitHub

Open Graph Title: Null-Check is not fully recognized for `Partial` generic mapped types · Issue #42537 · microsoft/TypeScript

X Title: Null-Check is not fully recognized for `Partial` generic mapped types · Issue #42537 · microsoft/TypeScript

Description: Bug Report 🔎 Search Terms mapped type null check function parameter 🕗 Version & Regression Information First encountered today, when attempting to write some code utilizing generic mapped types. This is the behavior in every version I tr...

Open Graph Description: Bug Report 🔎 Search Terms mapped type null check function parameter 🕗 Version & Regression Information First encountered today, when attempting to write some code utilizing generic mapped types. Th...

X Description: Bug Report 🔎 Search Terms mapped type null check function parameter 🕗 Version & Regression Information First encountered today, when attempting to write some code utilizing generic mapped types...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Null-Check is not fully recognized for `Partial\u003cT>` generic mapped types","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\nmapped\r\ntype\r\nnull\r\ncheck\r\nfunction\r\nparameter\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nFirst encountered today, when attempting to write some code utilizing generic mapped types.\r\n\r\nThis is the behavior in every version I tried, including 3.3.3 through Nightly, and I reviewed the FAQ for entries about mapped types.\r\n\r\n### ⏯ Playground Link\r\n\r\n[Here](https://www.typescriptlang.org/play?ts=4.2.0-dev.20210128#code/C4TwDgpgBAYg9nAPAFQIIDsDGALOAnKCAD2AnQBMBnKACWQFkAZAUQBsIBbM4APgCgogqAF4oAbwFCpeCAENycdKxBRZWXHgBcUNOvwBuSYIC+hvqEiwE9WWCMjx96XIVKVAbQBmCANIQQ2pTAeACW6ADmALra8Eh0TGyc3DyGUsZ85uDQsRg4+DZgKAWEJGRUVnAF-FKiElLO8orKUO7IfiphUADW-nCeOgXRA7at7ZHuAERqeXgTkalCphmeAK5YwCGKUABGsnhFtiWkFNSxVQAU3nDt2j0gfcNgADRQV7kalNoACnsbsqyIHJ6PAFA5gHg8ACUjikmEUQVeCHe+Acb2BlC8vn88wyUhC-UuSOBIlE6BWrFY0Lq9UEwGweDgAHcoOgIMzmHgGXhzgADACq6Fk23YUGAcFeYXI3X8UAA5AASMRXdrGWVQTocWw8yELEy4oRomYAOmwIXI5DIDmCKwgZikuwAXoS4Mi8Dq+Ok+Kt1pt0DtZE7phptPEWOwuOhgFT7EH8CazRa-aJPP9KLaPXwgA)\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ntype Foo\u003cTAnchor extends HTMLElement> = { readonly anchor: TAnchor; };\r\ntype FooMap = { readonly [fooKey: string]: Foo\u003cHTMLElement>; };\r\ntype FooAnchorMap\u003cTMap extends FooMap> = { readonly [TKey in keyof TMap]: TMap[TKey][\"anchor\"]; };\r\n\r\nfunction baz(anchor: HTMLElement) { anchor.hidden = false; }\r\n\r\nfunction bar\u003cTMap extends FooMap>(fooKey: keyof TMap, fooAnchors: Partial\u003cFooAnchorMap\u003cTMap>>) {\r\n    const fooAnchor = fooAnchors[fooKey];\r\n\r\n    if (fooAnchor == null) {\r\n        throw new Error(`Unable to find key '${fooKey}' in map`);\r\n    }\r\n\r\n    // The compiler clearly recognizes that fooAnchor is HTMLElement, as confirmed by intellisense.\r\n    fooAnchor.hidden = true;\r\n\r\n    // Error here, indicating that the inferred type of fooAnchor is 'TMa[[keyof TMap][\"anchor\"] | undefined', as confirmed by intellisense\r\n    baz(fooAnchor);\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe variable `fooAnchor` is inferred to be possibly `undefined`, almost immediately after a clear null-check. However, it's somehow only inferred to be possibly `undefined` when passed as a function argument. When used directly, the null-check is recognized.\r\n\r\n### 🙂 Expected behavior\r\n\r\nIdeally, the null-check should be recognized fully, by all uses of `fooAnchor` after the null-check occurs. However, if this is somehow not feasible, the inferred type of `fooAnchor` should at least be consistent. I.E. it should not be possible for the compiler to infer the variable's differently at different points within the same narrowing scope (I.E. when no type-narrowing or type-widening operations have occurred).\r\n\r\nThis seems to be very closely related to https://github.com/microsoft/TypeScript/issues/31908. However, I thought it was appropriate to make a separate issue, because this situation doesn't completely line up with the situations described in that issue. Most significantly...\r\n\r\n> TypeScript doesn't know that partial[key] is equivalent to T[K] | undefined, only that it's assignable to that type.\r\n\r\nThis doesn't seem to be true here, because A) the error, and intellisense clearly report the inferred type to be `... | undefined` and B) the variable is able to be used directly as if it's not possibly `undefined`. It's only when passing the variable as an argument that the error occurs.\r\n","author":{"url":"https://github.com/JakenVeina","@type":"Person","name":"JakenVeina"},"datePublished":"2021-01-28T19:24:43Z","url":"https://github.com/microsoft/TypeScript/issues/42537"}

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:ed9633b2-bbe2-7d90-9346-4bbc66f2160b
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idEDB0:6365:D5ED9E:11E519B:6B01026A
html-safe-nonceb5b8c278fffe92a83d6ec67573112ae88691219e9930d5bf189f993fda5405a1
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREIwOjYzNjU6RDVFRDlFOjExRTUxOUI6NkIwMTAyNkEiLCJ2aXNpdG9yX2lkIjoiMzk0OTA4ODU5OTI4NjI4NDkwNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac54b31b14ce8d17627c17b77ce8aa6e5dd1ada8995bbb813070ce4a49b69b5b3a
hovercard-subject-tagissue:796282799
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/42537/issue_layout
twitter:imagehttps://opengraph.githubassets.com/39e57f3a1bad169240fd32780ba097035bd9678308f87d1d43b27f6cfa2ca791/microsoft/TypeScript/issues/42537
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/39e57f3a1bad169240fd32780ba097035bd9678308f87d1d43b27f6cfa2ca791/microsoft/TypeScript/issues/42537
og:image:altBug Report 🔎 Search Terms mapped type null check function parameter 🕗 Version & Regression Information First encountered today, when attempting to write some code utilizing generic mapped types. Th...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameJakenVeina
hostnamegithub.com
expected-hostnamegithub.com
Nonecb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124
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
release2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a
ui-targetcanary-2
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/42537#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F42537
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%2F42537
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/42537
Reloadhttps://github.com/microsoft/TypeScript/issues/42537
Reloadhttps://github.com/microsoft/TypeScript/issues/42537
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests309 (309)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/42537
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/42537
Null-Check is not fully recognized for Partial generic mapped typeshttps://github.com/microsoft/TypeScript/issues/42537#top
Design LimitationConstraints of the existing architecture prevent this from being fixedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Design%20Limitation%22
https://github.com/JakenVeina
Jake Meiergerd (JakenVeina)https://github.com/JakenVeina
on Jan 28, 2021https://github.com/microsoft/TypeScript/issues/42537#issue-796282799
Herehttps://www.typescriptlang.org/play?ts=4.2.0-dev.20210128#code/C4TwDgpgBAYg9nAPAFQIIDsDGALOAnKCAD2AnQBMBnKACWQFkAZAUQBsIBbM4APgCgogqAF4oAbwFCpeCAENycdKxBRZWXHgBcUNOvwBuSYIC+hvqEiwE9WWCMjx96XIVKVAbQBmCANIQQ2pTAeACW6ADmALra8Eh0TGyc3DyGUsZ85uDQsRg4+DZgKAWEJGRUVnAF-FKiElLO8orKUO7IfiphUADW-nCeOgXRA7at7ZHuAERqeXgTkalCphmeAK5YwCGKUABGsnhFtiWkFNSxVQAU3nDt2j0gfcNgADRQV7kalNoACnsbsqyIHJ6PAFA5gHg8ACUjikmEUQVeCHe+Acb2BlC8vn88wyUhC-UuSOBIlE6BWrFY0Lq9UEwGweDgAHcoOgIMzmHgGXhzgADACq6Fk23YUGAcFeYXI3X8UAA5AASMRXdrGWVQTocWw8yELEy4oRomYAOmwIXI5DIDmCKwgZikuwAXoS4Mi8Dq+Ok+Kt1pt0DtZE7phptPEWOwuOhgFT7EH8CazRa-aJPP9KLaPXwgA
#31908https://github.com/microsoft/TypeScript/issues/31908
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
Design LimitationConstraints of the existing architecture prevent this from being fixedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Design%20Limitation%22
on Jan 29, 2021https://github.com/microsoft/TypeScript/issues/42537#event-4267316656
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Jan 29, 2021https://github.com/microsoft/TypeScript/issues/42537#issuecomment-769955975
#22348https://github.com/microsoft/TypeScript/pull/22348
https://github.com/JakenVeina
Jake Meiergerd (JakenVeina)https://github.com/JakenVeina
on Jan 30, 2021https://github.com/microsoft/TypeScript/issues/42537#issuecomment-770136050
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Jun 1, 2021https://github.com/microsoft/TypeScript/issues/42537#event-4828216006
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/42537
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/42537
Design LimitationConstraints of the existing architecture prevent this from being fixedhttps://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

Viewport: width=device-width


URLs of crawlers that visited me.