Title: Union types (Foo | Bar) inconsistently becomes Partial
Open Graph Title: Union types (Foo | Bar) inconsistently becomes Partial
X Title: Union types (Foo | Bar) inconsistently becomes Partial
Description: 🔎 Search Terms "union type partial", "type union narrowing" 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Unions ⏯ Playground Link https://www.typescriptlang.or...
Open Graph Description: 🔎 Search Terms "union type partial", "type union narrowing" 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Unions ⏯ Playg...
X Description: 🔎 Search Terms "union type partial", "type union narrowing" 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entr...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/59819
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Union types (Foo | Bar) inconsistently becomes Partial\u003cFoo & Bar> ","articleBody":"### 🔎 Search Terms\r\n\r\n\"union type partial\", \"type union narrowing\"\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Unions\r\n\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://www.typescriptlang.org/play/?exactOptionalPropertyTypes=true&ts=5.5.4#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChkHIyYBcyAzmFKAOYDcuAvrrqJLIigEJxQ76EARrzIgArgFtB0BoWQAvOPLKVqIekxYwxIBGGDoQyYfIAUAByjpz5MhiwAfZDz4BKfnOAxkpgETCoX2RQZEtrcnc8OTkEQ3J0ABsIADoE9BoLKxtkxXlXWUJGZAgE8hQo6IJYkHik1PTM8OTidHyBAmZmXBNTbCJSZF9ISiDGfOQAegnkYbBupV7jEWQARgAmAGYAFgAaBSUyXwBPMQALUfGp5BPTlivMGwBCa-QxZAQ4I3M4cnJkOAoYkEZTAyHQ3n86DA52QUDg0OgM1On0GhhQ6D4CLBCMCyRYPT6LUOs18ewCZHWG2QYzok2mOgAJhAYKAIAz5mZCQMhhARntcocGeQYAyYCsLrSrrMgA\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ninterface Foo {\r\n foo: string;\r\n}\r\n\r\ninterface Bar {\r\n bar: number;\r\n zaz: string;\r\n}\r\n\r\nfunction baz(props: Foo | Bar ) {\r\n if (\"bar\" in props) {\r\n console.log(props.zaz);\r\n } else {\r\n console.log(props.foo);\r\n }\r\n}\r\n\r\nbaz({ foo: \"test\" }); // test\r\nbaz({ bar: 1234, zaz: \"yuh\" }); // yuh\r\n\r\n// oops! you can pass a subset of \"both\" rather than \"one or the other\".\r\n\r\nbaz({ foo: \"test\", bar: 123 }); // undefined\r\nbaz({ foo: \"test\", zaz: \"dsfdf1\" }); // test\r\n```\r\n\r\n\r\n### 🙁 Actual behavior\r\n\r\nI was surprised that I would pass in an object that is _neither_ `Foo` **nor** `Bar` and TypeScript didn't complain about. This caused a bug at runtime that it seems like TypeScript should have caught?\r\n\r\nInterestingly, the function definition is well aware that only one or the other could be passed. I could not write a scenario where that wasn't the case, but _calling_ the funciton is where this fails.\r\n\r\n### 🙂 Expected behavior\r\n\r\nI expected to be able to pass `Foo` **or** `Bar` but not _both_ and certainly not _neither_.\r\n\r\n### Additional information about the issue\r\n\r\nI couldn't find a previous version of TypeScript where this worked as I would expect it too. I was really wondering if I was doing something wrong or this was a well known issue, but I was struggling to find anything on the topic.\r\n\r\nIt looks like this also fails:\r\n```ts\r\nconst data: Foo|Bar = {\r\n foo: \"test\",\r\n bar: 123,\r\n};\r\n```\r\nin which case `data` is **neither** `Foo` _nor_ `Bar` but TypeScript doesn't complain about it. \r\n\r\nYou can see this with both of these that fail:\r\n```ts\r\nconst data: Foo = {\r\n foo: \"test\",\r\n bar: 123,\r\n};\r\nconst data: Bar = {\r\n foo: \"test\",\r\n bar: 123,\r\n};\r\n```\r\n\r\nI would think that if they fail seperately, a union should _also_ fail?","author":{"url":"https://github.com/davidbarratt","@type":"Person","name":"davidbarratt"},"datePublished":"2024-09-01T19:18:43Z","url":"https://github.com/microsoft/TypeScript/issues/59819"}
| 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:b591d8cc-2860-16dd-be63-f4c6f0ff7bdb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C6BA:311640:2D99706:3A013BF:6B01BDE9 |
| html-safe-nonce | 983c8391cd9d1f179478141e1316375f6c5c48fd88e7e3fa880c730d54685e72 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNkJBOjMxMTY0MDoyRDk5NzA2OjNBMDEzQkY6NkIwMUJERTkiLCJ2aXNpdG9yX2lkIjoiNzA5MDE1MDY2Mjc4MDA3NTQ5NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | be856e2cd70a91451ba52b82e8747fa8d3ea064d2ab500884d7df80d50a734e2 |
| hovercard-subject-tag | issue:2499640127 |
| 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/59819/issue_layout |
| twitter:image | https://opengraph.githubassets.com/592b5a73ca7a1459a9f4a5c0bd89655019b8caea880cd7f8d2cae8f0a602b8e1/microsoft/TypeScript/issues/59819 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/592b5a73ca7a1459a9f4a5c0bd89655019b8caea880cd7f8d2cae8f0a602b8e1/microsoft/TypeScript/issues/59819 |
| og:image:alt | 🔎 Search Terms "union type partial", "type union narrowing" 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Unions ⏯ Playg... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | davidbarratt |
| 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