Title: Conditional Type is assignable to a discriminated union, yet not working with type guards as expected. · Issue #23985 · microsoft/TypeScript · GitHub
Open Graph Title: Conditional Type is assignable to a discriminated union, yet not working with type guards as expected. · Issue #23985 · microsoft/TypeScript
X Title: Conditional Type is assignable to a discriminated union, yet not working with type guards as expected. · Issue #23985 · microsoft/TypeScript
Description: (Apologies if this is a duplicate; I suspect that it is after seeing so many related questions being marked "behaving as designed," but am having a hard time figuring out exactly which issue this may be duplicating). TypeScript Version: ...
Open Graph Description: (Apologies if this is a duplicate; I suspect that it is after seeing so many related questions being marked "behaving as designed," but am having a hard time figuring out exactly which issue this m...
X Description: (Apologies if this is a duplicate; I suspect that it is after seeing so many related questions being marked "behaving as designed," but am having a hard time figuring out exactly which is...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/23985
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Conditional Type is assignable to a discriminated union, yet not working with type guards as expected.","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n(Apologies if this is a duplicate; I suspect that it is after seeing so many related questions being marked \"behaving as designed,\" but am having a hard time figuring out exactly _which_ issue this may be duplicating).\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 2.8.3\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** discriminated union, conditional generic, etc...\r\n\r\n**Code**\r\n\r\n```ts\r\ninterface Foo {\r\n hasADate: true;\r\n obj: Date;\r\n}\r\n\r\ninterface Bar {\r\n hasADate: false;\r\n obj: number;\r\n}\r\n\r\ntype FooBar = Foo | Bar;\r\ntype GenericFooBar\u003cT> = T extends Function ? Foo : Bar;\r\n\r\nfunction testing\u003cT>(doesntWork: GenericFooBar\u003cT>) {\r\n if (doesntWork.hasADate === true) {\r\n return doesntWork.obj.getDate(); // ERROR: arg.obj is still type number | Date\r\n }\r\n\r\n let works: FooBar = doesntWork;\r\n if (works.hasADate === true) {\r\n return works.obj.getDate(); // WORKS: arg.obj is type Date\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:** \r\nInside of the `doesntWork.hasADate === true` type guard, `doesntWork.obj` should be narrowed to a `Date`\r\n\r\n**Actual behavior:** \r\n`doesntWork.obj` is still a `number | Date`. The compiler _does_ allow me to assign `doesntWork` to `works` (whose type is the respective discriminated union), and with that variable everything works as expected. It seems odd that the compiler recognizes that `GenericFooBar\u003cT>` extends `Foo | Bar` (as it allows the assignment), yet doesn't work with the discriminated type guard.\r\n\r\n","author":{"url":"https://github.com/DavidKDeutsch","@type":"Person","name":"DavidKDeutsch"},"datePublished":"2018-05-08T22:55:36Z","url":"https://github.com/microsoft/TypeScript/issues/23985"}
| 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:4902f3e0-199c-c2b3-bc6e-eddfc30b00ba |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E744:21AC3F:E3E12F:1238263:6B00F355 |
| html-safe-nonce | f14656868e9ba8321b31532490dc625c8deb94bdd32f42be22af23ccf3d40973 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzQ0OjIxQUMzRjpFM0UxMkY6MTIzODI2Mzo2QjAwRjM1NSIsInZpc2l0b3JfaWQiOiI3NzIxNzY0ODU5NjU3OTA5MDc3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b5f3e04d0e788eeea90d641961155003d3ddb7fe0944aa0f8d3a7df84652a2f9 |
| hovercard-subject-tag | issue:321374548 |
| 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/23985/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d35f6d75ed0795f59fbc8785a7f62bd674824289edb941393cf94bda69caff32/microsoft/TypeScript/issues/23985 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d35f6d75ed0795f59fbc8785a7f62bd674824289edb941393cf94bda69caff32/microsoft/TypeScript/issues/23985 |
| og:image:alt | (Apologies if this is a duplicate; I suspect that it is after seeing so many related questions being marked "behaving as designed," but am having a hard time figuring out exactly which issue this m... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DavidKDeutsch |
| 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 | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width