Title: Narrowing doesn't work with constant-named properties · Issue #48203 · microsoft/TypeScript · GitHub
Open Graph Title: Narrowing doesn't work with constant-named properties · Issue #48203 · microsoft/TypeScript
X Title: Narrowing doesn't work with constant-named properties · Issue #48203 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms narrowing, constant-named properties, computed property names 🕗 Version & Regression Information 3.3.3 This is the behavior in every version I tried, and I reviewed the FAQ for entries about "common bugs that ar...
Open Graph Description: Bug Report 🔎 Search Terms narrowing, constant-named properties, computed property names 🕗 Version & Regression Information 3.3.3 This is the behavior in every version I tried, and I reviewed the FA...
X Description: Bug Report 🔎 Search Terms narrowing, constant-named properties, computed property names 🕗 Version & Regression Information 3.3.3 This is the behavior in every version I tried, and I reviewed th...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/48203
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Narrowing doesn't work with constant-named properties","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\nnarrowing, constant-named properties, computed property names\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n3.3.3\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about \"common bugs that aren't bugs\"\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=3.3.3#code/PTAEBcAsEsGdQCYHsCmsB2BycoDuSAnAawCgAzAV3QGNxol1R0lwB9fY6dAc1dnAJdeAG2jgUBAIbCAFAEpQAbxKhVoag36hWZJElABeUJh17MAbhIq14AJ4AHFKABiew0tCTQALiYUAtgBGEqAAvpZqEA5OAEKSBO6KoADapkgAugD8vq764VaRGuhagfG+cQlGSam6Gb5Jkr4AjGFhltaq0GQypQQ1eukKvf0ZAHReRgDMlqFWIBAw8MhoeISklDR0DKucPHwCQqyi4lKyCsqFmjhp7ia1Fh1Rji5uVZ4+fkEh+ZF2zxWJFJpLI5Nz5R5FEplUAAt4jdL1TzNVrgyJdHrxZJ3MyDUDDbFITDpcbuaYkWYkeZQOCeYSwfTLDDYHbrKi0eiMZhsDhEQ5UaAARwoKD4tiCSDp8iUEKu2lq7gAymLAhKZATMHJ2r9oi99G8vL50AFggkfjYdbCPPDsrq2gU1JCcL1yvFAfDEY1QC1QnbHujhsChpjgSSpjMrEA)\r\n\r\n### 💻 Code\r\n\r\n```ts\r\n// this doesn't work\r\nfunction not_working_string_literal() {\r\n const _foo = '_foo';\r\n\r\n type Foo = { a : number };\r\n type Bar = { [_foo]?: Foo };\r\n\r\n const bar: Bar = { [_foo]: { a: 1 } };\r\n\r\n if(bar[_foo]) bar[_foo].a = 3;\r\n}\r\n\r\n// this does work\r\nfunction working_string_literal() {\r\n const _foo = '_foo';\r\n type Foo = { a : number };\r\n type Bar = { [_foo]?: Foo };\r\n\r\n const bar: Bar = { [_foo]: { a: 1 } };\r\n\r\n if(bar['_foo']) bar['_foo'].a = 3;\r\n}\r\n\r\n// this also doesn't work\r\nfunction not_working_unique_symbols() {\r\n const _foo = Symbol('_foo');\r\n\r\n type Foo = { a : number };\r\n type Bar = { [_foo]?: Foo };\r\n\r\n const bar: Bar = { [_foo]: { a: 1 } };\r\n\r\n if(bar[_foo]) bar[_foo].a = 3;\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nLine 10 fails to type check with the error:\r\n```\r\nObject is possibly 'undefined'.\r\n```\r\n\r\n### 🙂 Expected behavior\r\n\r\nI expect the narrowing `if(bar[_foo])` to remove the possibility that `bar[_foo]` is undefined inside the body of the if statement.\r\n","author":{"url":"https://github.com/jluxenberg","@type":"Person","name":"jluxenberg"},"datePublished":"2022-03-10T18:23:50Z","url":"https://github.com/microsoft/TypeScript/issues/48203"}
| 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:923dd34e-3339-7521-f3ef-8bc7d95f9460 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CA0A:1B1760:5B0433:7BFFA3:6B04C80F |
| html-safe-nonce | 50872f2fc7ca873802331e32fa07402ab5c8b37fb886631822fc37a08669ae75 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQTBBOjFCMTc2MDo1QjA0MzM6N0JGRkEzOjZCMDRDODBGIiwidmlzaXRvcl9pZCI6IjgyMDEyOTA0Njc4MzM1OTU5MTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5b943a780e38e8e374cb51508c350f6108bcb492f86db6004879d6ab9a63a453 |
| hovercard-subject-tag | issue:1165569441 |
| 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/48203/issue_layout |
| twitter:image | https://opengraph.githubassets.com/92c364bb567bee4b07e0d73843be68f4702f0a332b960e3ec2715f7b3eac34ae/microsoft/TypeScript/issues/48203 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/92c364bb567bee4b07e0d73843be68f4702f0a332b960e3ec2715f7b3eac34ae/microsoft/TypeScript/issues/48203 |
| og:image:alt | Bug Report 🔎 Search Terms narrowing, constant-named properties, computed property names 🕗 Version & Regression Information 3.3.3 This is the behavior in every version I tried, and I reviewed the FA... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jluxenberg |
| 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