Title: [5.3] Type narrowing regression with property comparison to boolean false · Issue #56482 · microsoft/TypeScript · GitHub
Open Graph Title: [5.3] Type narrowing regression with property comparison to boolean false · Issue #56482 · microsoft/TypeScript
X Title: [5.3] Type narrowing regression with property comparison to boolean false · Issue #56482 · microsoft/TypeScript
Description: 🔎 Search Terms narrowing, strict equals, property access, boolean false, undefined 🕗 Version & Regression Information This changed between versions 5.2 and 5.3 This also happens in the nightly ⏯ Playground Link https://www.typescriptlang...
Open Graph Description: 🔎 Search Terms narrowing, strict equals, property access, boolean false, undefined 🕗 Version & Regression Information This changed between versions 5.2 and 5.3 This also happens in the nightly ⏯ Pl...
X Description: 🔎 Search Terms narrowing, strict equals, property access, boolean false, undefined 🕗 Version & Regression Information This changed between versions 5.2 and 5.3 This also happens in the nightly ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/56482
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[5.3] Type narrowing regression with property comparison to boolean false","articleBody":"### 🔎 Search Terms\r\n\r\nnarrowing, strict equals, property access, boolean false, undefined\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This changed between versions 5.2 and 5.3\r\n- This also happens in the nightly\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://www.typescriptlang.org/play?ts=5.3.1-rc#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChnIBGcUAXEZgDYRwi4C+uuC6IAzmMgOYRgZYBeZAAoAlOX7IAPsgCuIACYQYoCAuQCAfDnzIovWVBDIAsnDAALAHRRaC9AFsxybQAYrAVmQB+HERLk8JRsKPTI5PJKKiBqDEws7JwwmBrcvPxiuMAwIsno3lbEUBoCQkEhojoECWzo1FaU6FzCeaJxuAD0HcgAAmBsALQQAB4ADhAIYENQUOjFA8h5yMBsyKPobGzAhJQAnnKKyqoKzKy19Y3NeYUkbUA\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ninterface Foo {\r\n bar: boolean\r\n}\r\n\r\nconst getFoo = (): Foo | undefined => {\r\n return Math.random() > 0.5 ? { bar: false } : undefined\r\n}\r\n\r\nconst foo = getFoo()\r\nif (foo?.bar === false) {\r\n console.log(foo)\r\n}\r\n\r\n// @ts-expect-error - foo is possibly undefined\r\nconsole.log(foo.bar)\r\n```\r\n\r\n\r\n### 🙁 Actual behavior\r\n\r\nIn line 13 and following, `foo` is incorrectly narrowed to just `Foo` (whereas it was previously `Foo | undefined`), although we learned nothing about it from the preceding `if` as that did not influence control flow.\r\n\r\n```\r\nUnused '@ts-expect-error' directive.(2578)\r\n```\r\n\r\n### 🙂 Expected behavior\r\n\r\nTS should report an error when accessing `foo.bar` as `foo` can potentially be undefined.\r\n\r\n### Additional information about the issue\r\n\r\nI originally discovered this issue in JSX with a snippet similar to:\r\n\r\n```tsx\r\nfunction Component () {\r\n const foo = getFoo()\r\n return (\r\n \u003cdiv>\r\n {foo?.bar === false && 'foo'}\r\n {foo.bar ? 'true' : 'false'}\r\n \u003c/div>\r\n )\r\n}\r\n```\r\n\r\nThere as well, the access `foo.bar` is allowed due to the preceding check, although they are completely separate.\r\n\r\nNote that changing it to `=== true` or removing the checks entirely fixes the problem.","author":{"url":"https://github.com/meyfa","@type":"Person","name":"meyfa"},"datePublished":"2023-11-21T13:50:36Z","url":"https://github.com/microsoft/TypeScript/issues/56482"}
| 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:c80828cc-482b-663c-4e08-bdf0ccbf4e3d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B6FC:3CBB3E:1B79EB0:2300A1A:6B01984F |
| html-safe-nonce | c5fab9d887995f329cfd3fe14fae20c8cde7561731bbf5b9f1b869ad4e0e44a2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNkZDOjNDQkIzRToxQjc5RUIwOjIzMDBBMUE6NkIwMTk4NEYiLCJ2aXNpdG9yX2lkIjoiOTA2MDk4MDA5ODI4MjM5NTcyNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1b3834cd774d7792e0920f220508b0179f98ec0afbce812e204869f7ea5d84eb |
| hovercard-subject-tag | issue:2004334281 |
| 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/56482/issue_layout |
| twitter:image | https://opengraph.githubassets.com/13215719f09489f32108ca2bb9eb9365023d218dc61ee175587d10f29cb7ff0f/microsoft/TypeScript/issues/56482 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/13215719f09489f32108ca2bb9eb9365023d218dc61ee175587d10f29cb7ff0f/microsoft/TypeScript/issues/56482 |
| og:image:alt | 🔎 Search Terms narrowing, strict equals, property access, boolean false, undefined 🕗 Version & Regression Information This changed between versions 5.2 and 5.3 This also happens in the nightly ⏯ Pl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | meyfa |
| 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