Title: Null-Check is not fully recognized for `Partial
Open Graph Title: Null-Check is not fully recognized for `Partial
X Title: Null-Check is not fully recognized for `Partial
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
Domain: github.com
{"@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-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:ed9633b2-bbe2-7d90-9346-4bbc66f2160b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EDB0:6365:D5ED9E:11E519B:6B01026A |
| html-safe-nonce | b5b8c278fffe92a83d6ec67573112ae88691219e9930d5bf189f993fda5405a1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREIwOjYzNjU6RDVFRDlFOjExRTUxOUI6NkIwMTAyNkEiLCJ2aXNpdG9yX2lkIjoiMzk0OTA4ODU5OTI4NjI4NDkwNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 54b31b14ce8d17627c17b77ce8aa6e5dd1ada8995bbb813070ce4a49b69b5b3a |
| hovercard-subject-tag | issue:796282799 |
| 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/42537/issue_layout |
| twitter:image | https://opengraph.githubassets.com/39e57f3a1bad169240fd32780ba097035bd9678308f87d1d43b27f6cfa2ca791/microsoft/TypeScript/issues/42537 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/39e57f3a1bad169240fd32780ba097035bd9678308f87d1d43b27f6cfa2ca791/microsoft/TypeScript/issues/42537 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JakenVeina |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width