Title: Value remains undefined after if check when accessed by key of an object · Issue #48035 · microsoft/TypeScript · GitHub
Open Graph Title: Value remains undefined after if check when accessed by key of an object · Issue #48035 · microsoft/TypeScript
X Title: Value remains undefined after if check when accessed by key of an object · Issue #48035 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms Value remains undefined after if check when accessed by key of an object 🕗 Version & Regression Information TypeScript v4.5.5 ⏯ Playground Link Playground link with relevant code 💻 Code // Setup interface Foo { ...
Open Graph Description: Bug Report 🔎 Search Terms Value remains undefined after if check when accessed by key of an object 🕗 Version & Regression Information TypeScript v4.5.5 ⏯ Playground Link Playground link with releva...
X Description: Bug Report 🔎 Search Terms Value remains undefined after if check when accessed by key of an object 🕗 Version & Regression Information TypeScript v4.5.5 ⏯ Playground Link Playground link with re...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/48035
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Value remains undefined after if check when accessed by key of an object","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\nValue remains undefined after if check when accessed by key of an object\r\n\r\n### 🕗 Version & Regression Information\r\nTypeScript v4.5.5\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.5.4#code/PTAEGUFMBcFcAcBQBLAdtSAnAZgQwMaSgBiA9qaAN6Ki2gBGumA-AFygDO0maA5gNw06jAF5tO3PoIC+iRPlKouobOXZkKAXipDajTOwDk+wwBpE0wfMXKA1pACe7ew9LYS5UNuNNDVhUrQoADuuOgc4JKovF6gABRcBhI80QCUXgB8oAEcpAA2kAB0eaS8CdypVoggoADqABZYRNCNoFiYpJigpPj4sJgciKqkANouALqgAGRTIWHQEVFlw2OO45VyyO5xKxPplKC6c+GRKcvkqw7r-IeyKNvQDvCQbioXE16a3ol8hvuHdGOC1OfB27zWlVucmqYAA8qg8g45kjoBQALa4eygZBBYKdWzWQKgABuuDysEgsOwGliuzWglJ5Mp1M8MyBizOcUZFKpGkqQA)\r\n\r\n### 💻 Code\r\n```ts\r\n// Setup\r\ninterface Foo {\r\n bar?: string;\r\n baz?: string;\r\n}\r\n\r\nconst foo: Foo = {\r\n bar: 'bar',\r\n};\r\n\r\nconst key: keyof Foo = 'bar';\r\n\r\nconst wantsString = (str: string) => console.log(str);\r\n\r\n\r\n// Where the error occurs\r\nfoo[key] && wantsString(foo[key]);\r\n\r\nif (foo[key]) { \r\n wantsString(foo[key]); \r\n}\r\n\r\nif (typeof foo[key] === 'string') { \r\n wantsString(foo[key]); \r\n}\r\n\r\n\r\n// Only way to make it work\r\nconst valueOfFoo = foo[key];\r\nvalueOfFoo && wantsString(valueOfFoo);\r\n```\r\n\r\n### 🙁 Actual behavior\r\nYou receive this error:\r\n\r\n```\r\nfoo[key] && wantsString(foo[key]);\r\n ^\r\nArgument of type 'string | undefined' is not assignable to parameter of type 'string'.\r\n Type 'undefined' is not assignable to type 'string'.ts(2345)\r\n```\r\n\r\n### 🙂 Expected behavior\r\nThe check before validates that `foo[key]` is truthy and knows that it must be a string.\r\n","author":{"url":"https://github.com/Bloemendaal","@type":"Person","name":"Bloemendaal"},"datePublished":"2022-02-25T09:02:44Z","url":"https://github.com/microsoft/TypeScript/issues/48035"}
| 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:1078db77-91ed-c74c-ce71-c81ea571364a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8D42:200172:B2E179:E9C511:6B04D90C |
| html-safe-nonce | 834e96a6cb4bba8fc4b06d5d9757f56a42b02f27a2a52e2e92939b71e7814afa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDQyOjIwMDE3MjpCMkUxNzk6RTlDNTExOjZCMDREOTBDIiwidmlzaXRvcl9pZCI6IjQ5NDk0MjE2NDk3MTcxNTYxMDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 48a38ab44e3a0bd0c0da2a876ea80315f6f7569018f9d7421fc3e95f6f4eac18 |
| hovercard-subject-tag | issue:1150204519 |
| 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/48035/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3b36bd363c0fbc77bf17aca7ee7cbb2ed352078b90846077dc0b797aa78a9f34/microsoft/TypeScript/issues/48035 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3b36bd363c0fbc77bf17aca7ee7cbb2ed352078b90846077dc0b797aa78a9f34/microsoft/TypeScript/issues/48035 |
| og:image:alt | Bug Report 🔎 Search Terms Value remains undefined after if check when accessed by key of an object 🕗 Version & Regression Information TypeScript v4.5.5 ⏯ Playground Link Playground link with releva... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Bloemendaal |
| hostname | github.com |
| expected-hostname | github.com |
| None | 194dacb8acfd3e4d3ffb2704e0b1f10327f0dad690a7142acc7b0e0a2d073965 |
| 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 | 6c35540a0a2991ee20bd053ba2601cce33f72cb1 |
| 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