Title: TypeScript fails to narrow out `undefined` via `typeof` check on generic indexed access · Issue #50548 · microsoft/TypeScript · GitHub
Open Graph Title: TypeScript fails to narrow out `undefined` via `typeof` check on generic indexed access · Issue #50548 · microsoft/TypeScript
X Title: TypeScript fails to narrow out `undefined` via `typeof` check on generic indexed access · Issue #50548 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms undefined narrowed 2322 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about 'undefined' (including via Find on Page) ⏯ Playground Link Playg...
Open Graph Description: Bug Report 🔎 Search Terms undefined narrowed 2322 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about 'undefined' (including v...
X Description: Bug Report 🔎 Search Terms undefined narrowed 2322 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about 'undefined' ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/50548
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypeScript fails to narrow out `undefined` via `typeof` check on generic indexed access","articleBody":"# Bug Report\r\n### 🔎 Search Terms\r\nundefined narrowed 2322\r\n\r\n### 🕗 Version & Regression Information\r\n- This is the behavior in every version I tried, and I reviewed the [FAQ](https://github.com/Microsoft/TypeScript/wiki/FAQ) for entries about 'undefined' (including via Find on Page)\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBAysCGAnO9gGcoF4oG8CwAUFMVALbxpoBcUAdgK6kBGEiA3ISVGvYgGbwAxhAAqEUpESpeEGg2asOREmAA28WhHRzGLdoQC+hUJCgAFdZuAp0WXJxLlKOhfuXFSAe0+1qdXYoOxJ6ITACWCKpmrGGeACYuekpcIeGRAJK0gqphtKixtImBBEYEucCsAsLmllpIIABC8WEQaACy8GD27lAA5BYaWr1QNANWNmjJJL0oiMM0sxNKpYI+aMBQAOZaNgBiiJ6kMFp2fPRZwAUAPEFQIgByUBAAHhW0cRgA1hAgnnw1gwQiEazVaHTAABpCAA+AAUt3WqGOG1GSEu8FUVzGdWBTTiLXanQA2g8ALrQqE9RHAe7wUjQGjfX7-EwQP7cBDWLSUgCUI38rm6XFWvg2YE8b3RqgASq16KoNthqciidTafTSVNiAB6bUAQTi+NomygiAgcXOcQ0GwABgAyO1QcWSsIY2U8BVQACEmGwlogfFy5ptUGAnlDAAtoCL8ZcfBjKVxdcAo09EAdEGRWmh4NsoIIIxpthgWMAAO4QCC0KAANwALAA6ADsDbrUA0cVrjYAHA2AAwAWlL8FuYT4sNZ7OdVal7vlGx92F6-sDmjivT5+B6XGntFncoVWq4UF1AAlPDXWO2mBfoGHuJXUYgpVjakCQfiwcSydCiUz2di754gS4IkvcpKkrcSbagAoumIShmgsIAEwAMzIchfIsKonhljQdzgNA-RvvUwFfmAYGkn+PwASRuKgoSFFkqSUAAD5QCuQbrlAYQYLQErtpQYSbHkTCqHe4asn08h6L0DZQSQuoAOpRmaUBxJ4rSRqgTyvFIUA2uxnFriGqz0lAfAHKQ7Z8BUmaFjWuQmnk8EVp2KZ3oRUDsh5BkiusNoAPwKcQZrALw1a7vuHrAEeBhPKoaDQFux4kGFEVQH2cWGIQQA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\ntype StarStats = {\r\n mass: number;\r\n surfaceTemperature: number;\r\n planets: number;\r\n}\r\ntype PlanetStats = {\r\n mass: number;\r\n moons: number;\r\n orbitalPeriod: number;\r\n orbitalInclination: number;\r\n}\r\ninterface PlanetaryBodiesMap {\r\n 'Planet' : PlanetStats;\r\n 'Star' : StarStats;\r\n}\r\nconst getStatFromSet = function\u003c\r\n TN extends keyof PlanetaryBodiesMap,\r\n>(\r\n statSet : Partial\u003cPlanetaryBodiesMap[TN]>,\r\n statName : keyof typeof statSet,\r\n) : number {\r\n const potentialResult = statSet[statName];\r\n //Adding redundant `&& potentialResult !== undefined` to the conditional,\r\n //the error message changes between v4.7.4 and v4.8.0-beta\r\n if(typeof potentialResult !== 'undefined') {\r\n potentialResult;\r\n //Hover above to see: Partial\u003cPlanetaryBodiesMap[TN]>[keyof PlanetaryBodiesMap[TN]]\r\n //Error ts(2322) below: Type 'PlanetaryBodiesMap[TN][keyof PlanetaryBodiesMap[TN]] | undefined' is not assignable to type 'number'.\r\n //Where does that extra `| undefined` come from after having narrowed the type of the `const`?\r\n return potentialResult;\r\n } else {\r\n return 0;\r\n }\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\nTypeScript complains that a constant value can be `undefined` within a conditional block where that is already checked for and excluded.\r\n\r\n### 🙂 Expected behavior\r\n\r\n1. Inside a conditional which can only be entered if a constant type is not `undefined`, TypeScript excludes ` | undefined` from the possible types of the constant. \r\n2. When showing the type of a constant as X in an error message about how that can't be assigned to Y, the type X should be the same as the type shown when hovering over the type. In this example, they differ by `| undefined`, which matters. In the unsimplified version where the type is more complex, the type of the constant shown on hover is very linguistically different from the type shown in the error, making this issue harder to debug. \r\n","author":{"url":"https://github.com/wbt","@type":"Person","name":"wbt"},"datePublished":"2022-08-30T19:36:24Z","url":"https://github.com/microsoft/TypeScript/issues/50548"}
| 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:a5dcf5cc-56f5-22d8-5dd1-8edfaa236e0a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D7E2:1D20B7:222A6DB:2B53DF6:6B01B16F |
| html-safe-nonce | cd29a447af4b5f98c2e5ab6e7b031478ffcf0b3210561688e1cd68491728259e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEN0UyOjFEMjBCNzoyMjJBNkRCOjJCNTNERjY6NkIwMUIxNkYiLCJ2aXNpdG9yX2lkIjoiNTU5NjE3NTQyNTkyNDYwODM2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a11e8ed6f53fcd040b4be9120f4b0b7418426f2574599507f68c2d220240ad16 |
| hovercard-subject-tag | issue:1356214334 |
| 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/50548/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9730382689824306c647473795d8bc22353452c56b42a7c9ee33838cbda4137b/microsoft/TypeScript/issues/50548 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9730382689824306c647473795d8bc22353452c56b42a7c9ee33838cbda4137b/microsoft/TypeScript/issues/50548 |
| og:image:alt | Bug Report 🔎 Search Terms undefined narrowed 2322 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about 'undefined' (including v... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wbt |
| 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