Title: Optional chaining, proper infer in type guard · Issue #34974 · microsoft/TypeScript · GitHub
Open Graph Title: Optional chaining, proper infer in type guard · Issue #34974 · microsoft/TypeScript
X Title: Optional chaining, proper infer in type guard · Issue #34974 · microsoft/TypeScript
Description: TypeScript Version: 3.7.0 Search Terms: optional chaining Code type X = { y?: { z?: string } } const x: X = { y: { } } // type guard function isNotNull(x: A): x is NonNullable { return x!== null && x!== undefined; } // function whi...
Open Graph Description: TypeScript Version: 3.7.0 Search Terms: optional chaining Code type X = { y?: { z?: string } } const x: X = { y: { } } // type guard function isNotNull(x: A): x is NonNullable { return x!== n...
X Description: TypeScript Version: 3.7.0 Search Terms: optional chaining Code type X = { y?: { z?: string } } const x: X = { y: { } } // type guard function isNotNull<A>(x: A): x is NonNullable<A> { r...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/34974
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Optional chaining, proper infer in type guard","articleBody":"**TypeScript Version:** 3.7.0\r\n\r\n**Search Terms:** optional chaining\r\n\r\n**Code**\r\n\r\n```ts\r\ntype X = {\r\n y?: {\r\n z?: string\r\n }\r\n}\r\nconst x: X = {\r\n y: {\r\n }\r\n}\r\n// type guard\r\nfunction isNotNull\u003cA>(x: A): x is NonNullable\u003cA> {\r\n return x!== null && x!== undefined;\r\n}\r\n// function which I want to call in the result of the expression\r\nfunction title(str: string) {\r\n return str.length > 0 ? 'Dear ' + str : 'Dear nobody';\r\n}\r\n\r\nisNotNull(x?.y?.z) ? title(x.y.z) : null // type error - object is possibly undefined\r\n```\r\nThe code fix is by adding additional checks or additional temporary variables:\r\n```ts\r\n(x?.y?.z && isNotNull(x.y.z)) ? title(x.y.z) : null\r\n// or\r\nconst tmp = x?.y?.z\r\nisNotNull(tmp) ? title(tmp) : null\r\n```\r\n**Expected behavior:**\r\nTypeScript is able to infer that optional chaining was used as an argument, what means that typeguard is checking the whole chain.\r\n\r\n**Actual behavior:**\r\nTypeScript needs a code change in order to understand that optional chaining already checked other values from being null | undefined.\r\n","author":{"url":"https://github.com/maciejsikora","@type":"Person","name":"maciejsikora"},"datePublished":"2019-11-07T13:10:33Z","url":"https://github.com/microsoft/TypeScript/issues/34974"}
| 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:75c7094a-95d8-c541-9d66-ff1ee0d0f99c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AEB8:3FEC83:216460:2BA997:6B016702 |
| html-safe-nonce | 94d3b24bb29bebacf9ab400a8c1d9e757fff98df0879ceb37f1de34ba3eebbd7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRUI4OjNGRUM4MzoyMTY0NjA6MkJBOTk3OjZCMDE2NzAyIiwidmlzaXRvcl9pZCI6IjYxMTIzOTM4Njc0MjI2ODkwMjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 01b81e2acc78a56824d6c7a10932d629367fa09582d3ec6417b15063e2224768 |
| hovercard-subject-tag | issue:519257090 |
| 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/34974/issue_layout |
| twitter:image | https://opengraph.githubassets.com/300ae9e9e84e281ff31b9541c38a7c1e34843db7ef57333fb09c39bdcd62e8cd/microsoft/TypeScript/issues/34974 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/300ae9e9e84e281ff31b9541c38a7c1e34843db7ef57333fb09c39bdcd62e8cd/microsoft/TypeScript/issues/34974 |
| og:image:alt | TypeScript Version: 3.7.0 Search Terms: optional chaining Code type X = { y?: { z?: string } } const x: X = { y: { } } // type guard function isNotNull(x: A): x is NonNullable { return x!== n... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | maciejsikora |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width