Title: Unable to discriminate union type because of "Property does not exist" · Issue #28138 · microsoft/TypeScript · GitHub
Open Graph Title: Unable to discriminate union type because of "Property does not exist" · Issue #28138 · microsoft/TypeScript
X Title: Unable to discriminate union type because of "Property does not exist" · Issue #28138 · microsoft/TypeScript
Description: TypeScript Version: typescript@3.2.0-dev.20181025 Search Terms: union narrowing discriminate missing property field Code class Foo { private foo: string; } class Bar { private bar: number; } function fn(baz: { foo: Foo; bar: string; } | ...
Open Graph Description: TypeScript Version: typescript@3.2.0-dev.20181025 Search Terms: union narrowing discriminate missing property field Code class Foo { private foo: string; } class Bar { private bar: number; } functi...
X Description: TypeScript Version: typescript@3.2.0-dev.20181025 Search Terms: union narrowing discriminate missing property field Code class Foo { private foo: string; } class Bar { private bar: number; } functi...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/28138
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unable to discriminate union type because of \"Property does not exist\"","articleBody":"**TypeScript Version:** typescript@3.2.0-dev.20181025\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** union narrowing discriminate missing property field\r\n\r\n**Code**\r\n\r\n```ts\r\nclass Foo\r\n{\r\n private foo: string;\r\n}\r\n\r\nclass Bar\r\n{\r\n private bar: number;\r\n}\r\n\r\nfunction fn(baz: { foo: Foo; bar: string; } | { foo: Bar; })\r\n{\r\n if (typeof baz.bar != 'undefined')\r\n {\r\n testFoo(baz.foo);\r\n }\r\n}\r\n\r\nfunction testFoo(foo: Foo)\r\n{\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nNo Error. I can call `testFoo(baz.foo)` since the union type is properly narrowed.\r\n\r\n**Actual behavior:**\r\nProperty 'bar' does not exist on type 'Baz'. \r\n\r\nArgument of type 'Foo | Bar' is not assignable to parameter of type 'Foo'.\r\n Type 'Bar' is not assignable to type 'Foo'.\r\n Property 'foo' is missing in type 'Bar'.\r\n\r\n**Playground Link:** https://www.typescriptlang.org/play/index.html#src=class%20Foo%0D%0A%7B%0D%0A%20%20%20%20private%20foo%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Aclass%20Bar%0D%0A%7B%0D%0A%20%20%20%20private%20bar%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Afunction%20fn(baz%3A%20%7B%20foo%3A%20Foo%3B%20bar%3A%20string%3B%20%7D%20%7C%20%7B%20foo%3A%20Bar%3B%20%7D)%0D%0A%7B%0D%0A%20%20%20%20if%20(typeof%20baz.bar%20!%3D%20'undefined')%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20testFoo(baz.foo)%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Afunction%20testFoo(foo%3A%20Foo)%0D%0A%7B%0D%0A%7D\r\n\r\n**Related Issues:** I've looked around but found none.\r\n\r\nAlthough, in general, I do understand this should not work, I think it would help greatly if we had a way to discriminate on properties that are only in some of the members of a union.\r\n\r\nOne of the solutions could be to type properties, that are missing in some of the union members as `prop?: void`, or `prop?: never`, or `prop?: undefined`, or whatever makes sense in the type system.\r\n","author":{"url":"https://github.com/lemoinem","@type":"Person","name":"lemoinem"},"datePublished":"2018-10-25T20:39:44Z","url":"https://github.com/microsoft/TypeScript/issues/28138"}
| 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:16d89b71-c9aa-67f4-69c7-7bb885775868 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A982:613C2:2A9631:3B7977:6B047585 |
| html-safe-nonce | ba1ba0171cd49944ee94b6cdfd197b4ede2a25e1da2aa4ff57e03824786d79b2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOTgyOjYxM0MyOjJBOTYzMTozQjc5Nzc6NkIwNDc1ODUiLCJ2aXNpdG9yX2lkIjoiMjM3OTg4MTc4MzI2NjQwNzgxMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8d47fd34c097faaff12564586181e843fa8e0b54292cfb6c3a2f848309c0a716 |
| hovercard-subject-tag | issue:374123150 |
| 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/28138/issue_layout |
| twitter:image | https://opengraph.githubassets.com/14c973ca122e457f5bf21d322fb320f04a4a0c7460c2c6d2c6724f50673362d4/microsoft/TypeScript/issues/28138 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/14c973ca122e457f5bf21d322fb320f04a4a0c7460c2c6d2c6724f50673362d4/microsoft/TypeScript/issues/28138 |
| og:image:alt | TypeScript Version: typescript@3.2.0-dev.20181025 Search Terms: union narrowing discriminate missing property field Code class Foo { private foo: string; } class Bar { private bar: number; } functi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lemoinem |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9c099f1b85def81ca352464c276510dfef32deaf3c305404e65b8d270284a58b |
| 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 | cf73f57448d52b3a7b619195e182b33d3b3454ca |
| 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