Title: Type inference issue with spread and generics · Issue #37934 · microsoft/TypeScript · GitHub
Open Graph Title: Type inference issue with spread and generics · Issue #37934 · microsoft/TypeScript
X Title: Type inference issue with spread and generics · Issue #37934 · microsoft/TypeScript
Description: We notice the following issue described in this playground. TL;DR; The type inferred when merging two objects trough a generic function is always resolved to T & U which is inexact if T shares keys with U. The correct inferred type of co...
Open Graph Description: We notice the following issue described in this playground. TL;DR; The type inferred when merging two objects trough a generic function is always resolved to T & U which is inexact if T shares keys...
X Description: We notice the following issue described in this playground. TL;DR; The type inferred when merging two objects trough a generic function is always resolved to T & U which is inexact if T shares ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/37934
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type inference issue with spread and generics","articleBody":"We notice the following issue described in [this](https://typescript-v2-497.ortam.now.sh/dev/bug-workbench/?#code/C4TwDgpgBAglC8UDeUDaBrAXFAzsATgJYB2A5gLrZ5FlQC+AUKJFAEILJpa4EkXbEArgFsARhHz0oDBgBMIAYwA2AQ3zQFAe2J4oK7DADccxavVQtO4FFHZWxhgHpHUYAEYohHJ1QAPKrxklDw0pFAAPlBCYhKG9AyWuu4cKAB06SoANFDpqaL0DonWwhKkEADyogBWisA4HogAPAAq2QCqAHwAFPpQrTbYbQCUCB1QXWkZ2bn5dEMOzq4ATJ7ecABkbF0A7gAWhAq7qzYqskPZOLuagkqyNtAofgGhwdR8EVEi4vhxdNJFyw4JXwZUqNQUdTcPWyoiGMmY0AAsqUIC12mNECgGFAcVxPMQoABRXzKQTyRroCAgTQAMz62Up1LpnWCzQw5GMf02SGxuNQSnxRJJSjJqMZtKgbQZVIlzQ6wTa-I5DC5yF5OP5gvFdK6zQ+w1ZSv1RpVxgBwNB1Vq3ia-U6PWw-VskqG2GRINRdoxYwmOSmfry9HmMkWwBWXnxNIk6juj38IT4r0CYUi0W+vwS2iSAGYgSiwdboTZgwCAG7Pd6pr4SDjAbOpFSGIA) playground.\r\n\r\n# TL;DR;\r\n\r\nThe type inferred when merging two objects trough a generic function is always resolved to `T & U` which is inexact if `T` shares keys with `U`. \r\n\r\nThe correct inferred type of `const mergeObjects = \u003cA, B>(a: A, b: B) => ({ ...a, ...b });`\r\n\r\nshould be \r\n\r\n```\r\ntype Merge\u003cT, U> = { \r\n [k in Exclude\u003ckeyof T, keyof U>]: T[k];\r\n} & {\r\n [l in Exclude\u003ckeyof U, keyof T>]: U[l];\r\n} & {\r\n [l in keyof (T | U)]: T[l] | U[l] \r\n};\r\n\u003cA, B>(a: A, b: B) => Merge\u003cA, B>\r\n```\r\n\r\nAs shown in the playground, the correct type is inferred when using the spread operator with concrete types.\r\n","author":{"url":"https://github.com/flovilmart","@type":"Person","name":"flovilmart"},"datePublished":"2020-04-13T18:09:47Z","url":"https://github.com/microsoft/TypeScript/issues/37934"}
| 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:a38c346a-83d3-a04e-673b-28b3e034cfaf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C830:36A3E:2B48DCC:39C257C:6B01CBA4 |
| html-safe-nonce | 15d648a55d9d065b04067802834af8570ab1c66fd360ae1cbf27e46a0e99ee6c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODMwOjM2QTNFOjJCNDhEQ0M6MzlDMjU3Qzo2QjAxQ0JBNCIsInZpc2l0b3JfaWQiOiI1NDc2MTEzMDcyNjEzNjA4MzU2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | eb9cefdc54581b80205f107beb3a59c94daba457d697c1cf0911e03cfd5dc62f |
| hovercard-subject-tag | issue:599043719 |
| 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/37934/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b0818ba608c9395bd086d88b5aa8b527b22778302a6732f0d5e7510870bab20/microsoft/TypeScript/issues/37934 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b0818ba608c9395bd086d88b5aa8b527b22778302a6732f0d5e7510870bab20/microsoft/TypeScript/issues/37934 |
| og:image:alt | We notice the following issue described in this playground. TL;DR; The type inferred when merging two objects trough a generic function is always resolved to T & U which is inexact if T shares keys... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | flovilmart |
| 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