Title: Type guard should infer the type of parent object when applied on a property · Issue #42384 · microsoft/TypeScript · GitHub
Open Graph Title: Type guard should infer the type of parent object when applied on a property · Issue #42384 · microsoft/TypeScript
X Title: Type guard should infer the type of parent object when applied on a property · Issue #42384 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms Type guard, parent object, infer, inference ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime ...
Open Graph Description: Suggestion 🔍 Search Terms Type guard, parent object, infer, inference ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScrip...
X Description: Suggestion 🔍 Search Terms Type guard, parent object, infer, inference ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaS...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/42384
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type guard should infer the type of parent object when applied on a property","articleBody":"# Suggestion\r\n\r\n## 🔍 Search Terms\r\n\r\nType guard, parent object, infer, inference\r\n\r\n## ✅ Viability Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion\r\n\r\nTypescript should be able to infer the type of an object, if a type guard was checked on a property of the said object. Currently, Typescript does correctly infer the type of the property, but not its parent object.\r\n\r\n## 📃 Motivating Example\r\n\r\nThe following example is very common in data oriented design.\r\n\r\n```typescript\r\ninterface Type1 { list: string[] }\r\n\r\ninterface Type2 { list: { [key: string]: string } }\r\n\r\ndeclare const obj: Type1 | Type2;\r\n\r\nif(Array.isArray(obj.list)) {\r\n const list: string[] = obj.list; // WORKS\r\n const map: { [key: string]: string } = obj.list; // ERROR, as expected\r\n const objCasted: Type1 = obj; // ERROR, unexpectedly\r\n} else {\r\n const map: { [key: string]: string } = obj.list; // WORKS\r\n const list: string[] = obj.list; // ERROR, as expected\r\n const objCasted: Type2 = obj; // ERROR, unexpectedly\r\n}\r\n```\r\n\r\nThe following example works and that is good because it is an equally common case in this type of design. \r\n\r\n```typescript\r\ninterface Type3 { type: 'type3', data: boolean }\r\n\r\ninterface Type4 { type: 'type4', data: string }\r\n\r\ndeclare const obj2: Type3 | Type4;\r\n\r\nif(obj2.type === 'type3') {\r\n const objCasted: Type3 = obj2; // WORKS\r\n} else {\r\n const objCasted: Type4 = obj2; // WORKS\r\n}\r\n```\r\n\r\nSo I believe the type guards should work the same way. As far as I see, this does not cause any inconsistency in the language or the type system. It is an improvement without any downsides.\r\n\r\n## 💻 Use Cases\r\n\r\nSee the [full example](https://www.typescriptlang.org/play?ts=5.4.0-dev.20240107#code/FASwdgLgpgTgZgQwMZQAQBUCeAHKBGVAb1QBsQBnCALlUpnAHMBtAXVQF9hRJZEUMcUAExFSFaqKYBrKJhp1GLeRHpgGHDlwAmUJCQQw0SAPZhKqYwCMAVjSy4CAHwG4hAbi4g4ACgCCMGARMADoKf0DMbytrYLJKAEpUAF4U1BUAVyhEwmBUPNQTMwgxSmVVZjYkixtY8TdUAHoG1AB1AHkAJQBpAGVc-P68ptQAAQhyAFooAA9cJAgpgOMYQYLTcwBbBGwaYmlZMsVDtQ0q6NrKeuGAUQ6OzoAaVARyVBm56C1V1cLzaIBhF6fOyCAhnGxXZq3e4dJ7pMDvXSfEiYYDsN4kchoHL5NZFVBbHaSGRyWgqI5k8qnaoxOIQSGtTq9Li44ZjSaI+aLGDLH7rYp044VZI0i70xpQu6PZ6vTmfFn5X7FAFAqBaEGuEXRBnQ6XwuVqlForieHjwZBoexQADMoggghoAHJ7bhrY6nloEBAEDRLMZjCQoAgwJpuNBzfwrQAWO0O1DOwRR92oT3eoWhnR6AxGfk0oQam2oZzRjygHzRITBF1oFJVBOux3ZPn4lWUNUF23g6zuCWM7p9dFQTHY5t-GyAtvqlxQGNdnvDdr942m8N8S2CACssdwvv9geDofAq4t04AbNuoOnOMBM-pDHix9ZrQWt8XBKfS15vNXjHAadaq0EZJUkdP0AyDMBGyIUdlXHVUpytLcu2tBkWmWKRXnIcB+CjYIAE40QxLFoNxJUaQnYEzy1GwUN7NCYAw2hsLQXCCM4IA).","author":{"url":"https://github.com/gkurt","@type":"Person","name":"gkurt"},"datePublished":"2021-01-17T16:12:45Z","url":"https://github.com/microsoft/TypeScript/issues/42384"}
| 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:aac1c553-6746-d884-2b41-230c9b717b95 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED64:3F8AB1:C261BC:FAAA24:6B00EB89 |
| html-safe-nonce | baf34d943abb3a3b1d0163cd0a63e01cf2e7e71a8696a2e4a90298fc6923322c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDY0OjNGOEFCMTpDMjYxQkM6RkFBQTI0OjZCMDBFQjg5IiwidmlzaXRvcl9pZCI6IjY0Mjg1OTg2NjY5NDgzMDc4NDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9d8c19810beb86b539e0aea66c95ae623bbcbfab4a4d228a6b954757f2cb7516 |
| hovercard-subject-tag | issue:787738672 |
| 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/42384/issue_layout |
| twitter:image | https://opengraph.githubassets.com/09171478596fa499d49c5a694c6d94b87951748fb718b1bc941b7a1196f8c6c4/microsoft/TypeScript/issues/42384 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/09171478596fa499d49c5a694c6d94b87951748fb718b1bc941b7a1196f8c6c4/microsoft/TypeScript/issues/42384 |
| og:image:alt | Suggestion 🔍 Search Terms Type guard, parent object, infer, inference ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScrip... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gkurt |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width