Title: Narrowing with `typeof` and optional chaining does not work specifically with a union containing `boolean` unless wrapped in parentheses · Issue #51700 · microsoft/TypeScript · GitHub
Open Graph Title: Narrowing with `typeof` and optional chaining does not work specifically with a union containing `boolean` unless wrapped in parentheses · Issue #51700 · microsoft/TypeScript
X Title: Narrowing with `typeof` and optional chaining does not work specifically with a union containing `boolean` unless wrapped in parentheses · Issue #51700 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms typeof boolean narrow optional chaining 🕗 Version & Regression Information This changed between versions 4.8.4 and 4.9.3 ⏯ Playground Link Playground link with relevant code 💻 Code type WrappedStringOr
Open Graph Description: Bug Report 🔎 Search Terms typeof boolean narrow optional chaining 🕗 Version & Regression Information This changed between versions 4.8.4 and 4.9.3 ⏯ Playground Link Playground link with relevant co...
X Description: Bug Report 🔎 Search Terms typeof boolean narrow optional chaining 🕗 Version & Regression Information This changed between versions 4.8.4 and 4.9.3 ⏯ Playground Link Playground link with relevan...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/51700
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Narrowing with `typeof` and optional chaining does not work specifically with a union containing `boolean` unless wrapped in parentheses","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\ntypeof boolean narrow optional chaining\r\n\r\n### 🕗 Version & Regression Information\r\n- This changed between versions 4.8.4 and 4.9.3\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?ts=4.9.3#code/C4TwDgpgBA6gTgQzJAJgZWHAlgOwOYDycAPACoB8UAvFAN4BuCANgK4QD8AXFAM6a54AvlAA+dRqw7dSggNwBYAFBKAZixwBjYFgD2OKDhYBbAEYQ4BANYAKAO6JkEFN3hJUGbPiLFDp85TFDJiYASjolKCgsFShrUEgdGPs3J3YAOgk2KABCKhoAcj5PPHyw2gjIqDgIYBY4fSCmBUVIwQrq2vqoZMcUDOY2ZrblRTVNbT0oHRMAKwgtKzsHVBdlpw8Bb2m5rXJAlmCyiujY+IhE7rWUdMzoXIKigVLwlsqOuoaDporhyPeunqofqSIZKVTqLS6fRjSF6RaApyrFLofheEgAMQhExwAQMXyOrxOcXA5ySVxuAzueSghVRJQJlSqNQ+eOCzVa7WZAKuwMGSmG4PGUKgJh0OiYEAQOAAQggUEtkUjehs0cRReLJTjRKzQi9IkSzhcEddeVSHnTnuVXn8uZ82T9OZ19MbTaCRjDsSKxRKpeisAAPJwK3pK9x07zqn1a-aHPVRGLEhIJ40UyRhe40x74S0VG1OnXsqC-Jn5l23IZAA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\ntype WrappedStringOr\u003cT> = {value?: string} | {value?: T};\r\n\r\nfunction numberOk(wrapped: WrappedStringOr\u003cnumber> | null) {\r\n if (typeof wrapped?.value !== 'string') {\r\n return null;\r\n }\r\n return wrapped.value;\r\n}\r\n\r\nfunction booleanBad(wrapped: WrappedStringOr\u003cboolean> | null) {\r\n if (typeof wrapped?.value !== 'string') {\r\n return null;\r\n }\r\n // @ts-expect-error Unexpected type error here\r\n return wrapped.value;\r\n}\r\n\r\nfunction booleanFixed(wrapped: WrappedStringOr\u003cboolean> | null) {\r\n // This is semantically equivalent to above format (and gets auto-fixed to the above by formatters like Prettier).\r\n if (typeof (wrapped?.value) !== 'string') {\r\n return null;\r\n }\r\n // No type error here\r\n return wrapped.value;\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe type of `wrapped` is narrowed to exclude the `{value?: boolean}` case, but doesn't exclude the `null` case.\r\n\r\n### 🙂 Expected behavior\r\n\r\nThe type of of `wrapped` is narrowed to both exclude the `{value?: boolean}` case and also exclude the `null` case.\r\n","author":{"url":"https://github.com/MichaelMitchell-at","@type":"Person","name":"MichaelMitchell-at"},"datePublished":"2022-11-30T19:41:13Z","url":"https://github.com/microsoft/TypeScript/issues/51700"}
| 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:15f2ed31-28ae-2391-f9f9-a010b84a1acd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C10:2F3E36:5F8360:8239CE:6B04C921 |
| html-safe-nonce | 3e203e00fef9cc54c198d407c9f251a3ef7c2a208e13c6b8de9dded4f11822b0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzEwOjJGM0UzNjo1RjgzNjA6ODIzOUNFOjZCMDRDOTIxIiwidmlzaXRvcl9pZCI6IjYyNzg0MzE5NjQ0NDE5MjE4MjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 752fc2f5823e1e4cf24a016b46f45ea8a7b7f1fa75b8a1d0a49e797d40e053cf |
| hovercard-subject-tag | issue:1470132826 |
| 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/51700/issue_layout |
| twitter:image | https://opengraph.githubassets.com/35a77d654f653a3e7c216d20bed4c939862c41c54dcba28366cf44fc90f37f45/microsoft/TypeScript/issues/51700 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/35a77d654f653a3e7c216d20bed4c939862c41c54dcba28366cf44fc90f37f45/microsoft/TypeScript/issues/51700 |
| og:image:alt | Bug Report 🔎 Search Terms typeof boolean narrow optional chaining 🕗 Version & Regression Information This changed between versions 4.8.4 and 4.9.3 ⏯ Playground Link Playground link with relevant co... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | MichaelMitchell-at |
| hostname | github.com |
| expected-hostname | github.com |
| None | 85dd75377eb77313206b8fc108bcee6a3a029286d94bf32bf9b4426e4d92e0c4 |
| 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 | 0d64fa748a2bab9a5b587a967ac4da1cfa8da6dd |
| 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