Title: Cannot use matching indexes across objects with similar types · Issue #31445 · microsoft/TypeScript · GitHub
Open Graph Title: Cannot use matching indexes across objects with similar types · Issue #31445 · microsoft/TypeScript
X Title: Cannot use matching indexes across objects with similar types · Issue #31445 · microsoft/TypeScript
Description: TypeScript Version: Reproduced in the playground - i assume this is running latest. before running in the playground was running on 3.4.5 Search Terms: keys across objects cannot be compared type values at same indexes do not honor that ...
Open Graph Description: TypeScript Version: Reproduced in the playground - i assume this is running latest. before running in the playground was running on 3.4.5 Search Terms: keys across objects cannot be compared type v...
X Description: TypeScript Version: Reproduced in the playground - i assume this is running latest. before running in the playground was running on 3.4.5 Search Terms: keys across objects cannot be compared type v...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31445
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cannot use matching indexes across objects with similar types","articleBody":"\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n\r\n**TypeScript Version:** Reproduced in the playground - i assume this is running latest. before running in the playground was running on 3.4.5\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\nkeys across objects cannot be compared\r\ntype values at same indexes do not honor that keys can only assume one value at a time\r\n\r\n(I'm having a hard time describing this issue, so I may have failed to search for it effectively; forgive me if that's the case.)\r\n\r\n**Code**\r\n\r\n```ts\r\ntype A = { a: 'a' | 'b'; b: 'c' | 'd'; };\r\ntype B = { a: ('a' | 'b')[]; b: ('c' | 'd')[]; };\r\ntype C = { a: string; b: number };\r\ntype D = { a: string[]; b: number[]; }\r\n\r\n\r\nconst a: A = { a: 'a', b: 'c' };\r\nconst b: B = { a: [], b: [] };\r\nconst c: C = { a: 'asdf', b: 1234 };\r\nconst d: D = { a: [], b: [] };\r\n\r\n(['a', 'b'] as const).forEach(key => {\r\n b[key].push(a[key]);\r\n b[key] = [...b[key], a[key]];\r\n d[key].push(c[key]);\r\n d[key] = [...d[key], c[key]];\r\n});\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nAll of the options for adding the keys to the array in the second half of the example above should compile and work correctly.\r\n\r\n**Actual behavior:**\r\n\r\nThe LHS of the equation is considered an `&` type while the RHS is an `|` type, which makes them incompatible. These types are clearly correct and should work, but e.g. `b[key]` is consider a `never` for the purposes of push (`'a' | 'b' & 'c' |'d'`), while `d[key]` is considered a `string & number` for those purposes (not sure why this isn't also a `never`)\r\n\r\nThe important part of this issue is that none of the possible ways of using these types together compile. All possible variants create too strict of a typing for what needs to go into the array variant of the original type, even though the types of the objects at identical keys are compatible\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\n\r\n[playground link](https://www.typescriptlang.org/play/#src=type%20A%20%3D%20%7B%20a%3A%20'a'%20%7C%20'b'%3B%20b%3A%20'c'%20%7C%20'd'%3B%20%7D%3B%0D%0Atype%20B%20%3D%20%7B%20a%3A%20('a'%20%7C%20'b')%5B%5D%3B%20b%3A%20('c'%20%7C%20'd')%5B%5D%3B%20%7D%3B%0D%0Atype%20C%20%3D%20%7B%20a%3A%20string%3B%20b%3A%20number%20%7D%3B%0D%0Atype%20D%20%3D%20%7B%20a%3A%20string%5B%5D%3B%20b%3A%20number%5B%5D%3B%20%7D%0D%0A%0D%0A%0D%0Aconst%20a%3A%20A%20%3D%20%7B%20a%3A%20'a'%2C%20b%3A%20'c'%20%7D%3B%0D%0Aconst%20b%3A%20B%20%3D%20%7B%20a%3A%20%5B%5D%2C%20b%3A%20%5B%5D%20%7D%3B%0D%0Aconst%20c%3A%20C%20%3D%20%7B%20a%3A%20'asdf'%2C%20b%3A%201234%20%7D%3B%0D%0Aconst%20d%3A%20D%20%3D%20%7B%20a%3A%20%5B%5D%2C%20b%3A%20%5B%5D%20%7D%3B%0D%0A%0D%0A(%5B'a'%2C%20'b'%5D%20as%20const).forEach(key%20%3D%3E%20%7B%0D%0A%20%20%20%20b%5Bkey%5D.push(a%5Bkey%5D)%3B%0D%0A%20%20%20%20b%5Bkey%5D%20%3D%20%5B...b%5Bkey%5D%2C%20a%5Bkey%5D%5D%3B%0D%0A%20%20%20%20d%5Bkey%5D.push(c%5Bkey%5D)%3B%0D%0A%20%20%20%20d%5Bkey%5D%20%3D%20%5B...d%5Bkey%5D%2C%20c%5Bkey%5D%5D%3B%0D%0A%7D)%3B)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n","author":{"url":"https://github.com/ftrimble","@type":"Person","name":"ftrimble"},"datePublished":"2019-05-17T17:48:41Z","url":"https://github.com/microsoft/TypeScript/issues/31445"}
| 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:a2ae8d8e-f063-3850-4ae6-4ed37fbd643e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DA6C:27E5CD:9AA9C7:C93F19:6B00E1EA |
| html-safe-nonce | 85cb69b47c03cae436038dd5bf6bda1f7e2f0b935692b082040c82feea4405db |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQTZDOjI3RTVDRDo5QUE5Qzc6QzkzRjE5OjZCMDBFMUVBIiwidmlzaXRvcl9pZCI6IjI5MDYxMjI0NzUxMTQ5MTQyODIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | beeaa42ad47585cc79c46e537755eaa7df8cd46a5e52732945fef46bf983be81 |
| hovercard-subject-tag | issue:445550838 |
| 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/31445/issue_layout |
| twitter:image | https://opengraph.githubassets.com/be3d407f71c4d889853a7fd9ed4dd17926b1b2bc0699e702a8ffa07407bac471/microsoft/TypeScript/issues/31445 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/be3d407f71c4d889853a7fd9ed4dd17926b1b2bc0699e702a8ffa07407bac471/microsoft/TypeScript/issues/31445 |
| og:image:alt | TypeScript Version: Reproduced in the playground - i assume this is running latest. before running in the playground was running on 3.4.5 Search Terms: keys across objects cannot be compared type v... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ftrimble |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width