Title: `typeof ... === "undefined"` check on discriminated union of `undefined` and object type doesn't narrow correctly · Issue #50340 · microsoft/TypeScript · GitHub
Open Graph Title: `typeof ... === "undefined"` check on discriminated union of `undefined` and object type doesn't narrow correctly · Issue #50340 · microsoft/TypeScript
X Title: `typeof ... === "undefined"` check on discriminated union of `undefined` and object type doesn't narrow correctly · Issue #50340 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms typeof narrow, typeof discriminated union 🕗 Version & Regression Information Affects all playground versions. Please keep and fill in the line that best applies: --> This is the behavior in every version I tried...
Open Graph Description: Bug Report 🔎 Search Terms typeof narrow, typeof discriminated union 🕗 Version & Regression Information Affects all playground versions. Please keep and fill in the line that best applies: --> This ...
X Description: Bug Report 🔎 Search Terms typeof narrow, typeof discriminated union 🕗 Version & Regression Information Affects all playground versions. Please keep and fill in the line that best applies: -->...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/50340
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`typeof ... === \"undefined\"` check on discriminated union of `undefined` and object type doesn't narrow correctly","articleBody":"# Bug Report\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n### 🔎 Search Terms\r\n\r\n\u003c!--\r\n What search terms did you use when trying to find an existing bug report?\r\n List them here so people in the future can find this one more easily.\r\n-->\r\ntypeof narrow, typeof discriminated union\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nAffects all playground versions.\r\n\r\nPlease keep and fill in the line that best applies:\r\n-->\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries mentioning \"typeof\" or \"narrow\". Closest is this https://github.com/Microsoft/TypeScript/wiki/FAQ#type-guards \r\n\r\nThe FAQ entry is TODO, but I don't think it applies here, the union is not `&` 'd with the type guard type at all.\r\n\r\n### ⏯ Playground Link\r\n\r\n\u003c!--\r\n A link to a TypeScript Playground \"Share\" link which shows this behavior\r\n\r\n The TypeScript Workbench can be used for more complex setups, try\r\n https://www.typescriptlang.org/dev/bug-workbench/\r\n\r\n As a last resort, you can link to a repo, but these will be slower for us to investigate.\r\n-->\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/PTAEFkE9QYwewCYFNQHM5IM6gBZIE5JA)\r\n\r\n### 💻 Code\r\n\r\nhttps://www.typescriptlang.org/play?ts=4.8.0-beta#code/MYewdgzgLgBAhjAvDA3gX3hVBTATrkXALlThOlwEswBzNAGhl2wgFcAbKE1sAE2wBm1bLwwAfVDDwFiPfkLAjGzNpxIoARiTCsAthrxo0AKHbZYWmBWo0A3KfMxgRHfrz3KAgBRQAngAdsEAF4ADppQhgAQkRkAHI5QWFeOIBKVGMYLJgNJDCI3FC4WxgAelKYMDh8EAB3EWMMbHYIbAzspzy4UJUOKFCNEvLKkFgqmvreRuNPL26C6NiYRIURdJRM7NzkeZrC4rKK8YJJxqkWto2O4C6elj6BoaPqk4a0IA\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\nconst a = {} as {error: {a: string}, result: undefined} | { error:undefined, result: {b: number}}\r\nlet b: string;\r\nlet c:number;\r\nif(typeof a.error !== 'undefined') {\r\n b = a.error.a; // narrowed\r\n} else {\r\n c = a.result.b; // not narrowed\r\n}\r\nif(a.error !== undefined) {\r\n b = a.error.a; // narrowed\r\n} else {\r\n c = a.result.b; // narrowed\r\n}\r\n```\r\n\r\nA similar example that I also expected to work but actually fails for both cases:\r\nhttps://www.typescriptlang.org/play?ts=4.8.0-beta#code/MYewdgzgLgBAhjAvDA3gX3hVBTATrkXALlThOlwEswBzNAGhl2wgFcAbKEsVgWwCM8MDAB9UMPAWIweAvI2ZtOJFP3JQqtNGgBQ7bLDUwK1GgG49BmMCKzBuC-tiUIAOT72kMABQAPEqxgANZgIADuYACUJL4wLjIeQogAfDBQAJ4ADtggAGYwsYhFMADkdnglFpS53hnZefAAdJKEMACExWWJuCWRqDowgzD8XnDN+ISNcGYwAPSzMnATYdgAJjoY2OwQ2P1D1qONihxQjfwz8zIgsGBLBCvrutXebS7ucrjeYy24kX0oAyGI2Q3wmuCmFwWt2Waw2Em2uwB+2Ah2OnDOkKuNzu4VhaCAA\r\n\r\n```ts\r\nconst a = {} as {error: {a: string}, result: number } | { error: number, result: {b: string}}\r\nlet b: string;\r\nlet c:number;\r\nlet isNumber = (x: unknown): x is number => typeof x === 'number';\r\nif(typeof a.error !== 'number') {\r\n b = a.error.a; // narrowed\r\n} else {\r\n c = a.result.b; // not narrowed\r\n}\r\nif(!isNumber(a.error)) {\r\n b = a.error.a; // narrowed\r\n} else {\r\n c = a.result.b; // not narrowed\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\ntypeof based narrowing only narrowed one code flow branch.\r\n\r\n### 🙂 Expected behavior\r\n\r\ntypeof based narrowing should narrow both sides of the branch.","author":{"url":"https://github.com/fmmoret","@type":"Person","name":"fmmoret"},"datePublished":"2022-08-17T18:44:27Z","url":"https://github.com/microsoft/TypeScript/issues/50340"}
| 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:2c886d23-8f01-0f8a-099b-1f3a6fac9a9b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED30:3F539E:632C4D:87EABA:6B04762B |
| html-safe-nonce | 10cf5af9584e5c4335fcc613c9bcb7d1740be4eed6a4baa25c1463fced550349 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDMwOjNGNTM5RTo2MzJDNEQ6ODdFQUJBOjZCMDQ3NjJCIiwidmlzaXRvcl9pZCI6IjEzNjc5NzE5NzA2MTE5Njc1MzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4061683eb76909913559160498f44f8b00100aab17d018db96cc76446caafcfd |
| hovercard-subject-tag | issue:1342142973 |
| 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/50340/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f7048d27fcba68b33430cc2924cadf1a3786f7127d73cb6a9200855907310edb/microsoft/TypeScript/issues/50340 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f7048d27fcba68b33430cc2924cadf1a3786f7127d73cb6a9200855907310edb/microsoft/TypeScript/issues/50340 |
| og:image:alt | Bug Report 🔎 Search Terms typeof narrow, typeof discriminated union 🕗 Version & Regression Information Affects all playground versions. Please keep and fill in the line that best applies: --> This ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fmmoret |
| 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