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
Domain: github.com
IPv4 address: 140.82.113.3
{"@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-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:c3035d45-7003-1ec6-410d-7a53a7713e6e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D62C:E08F0:5658CF:79CF27:6B04762C |
| html-safe-nonce | 521e8d9a9027e2fcbf939b80297e7770a7332e5553cd3a5bfe4f5444ee03f607 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENjJDOkUwOEYwOjU2NThDRjo3OUNGMjc6NkIwNDc2MkMiLCJ2aXNpdG9yX2lkIjoiNzY1NTY4Mzg1NDM0NjUxNjAxMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5a90ab9a2e7e652bf1566a68bf2e3c3458cf262e64a91e847334ab259413fbc4 |
| hovercard-subject-tag | issue:1558808061 |
| 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/52440/issue_layout |
| twitter:image | https://opengraph.githubassets.com/508aacc48d8416aec606d855882861b27b7d83307bdd42da9599773b1d11cc99/microsoft/TypeScript/issues/52440 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/508aacc48d8416aec606d855882861b27b7d83307bdd42da9599773b1d11cc99/microsoft/TypeScript/issues/52440 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wbt |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9c099f1b85def81ca352464c276510dfef32deaf3c305404e65b8d270284a58b |
| 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 | cf73f57448d52b3a7b619195e182b33d3b3454ca |
| 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