Title: Rest type parameter in generics for use with intersection types · Issue #3870 · microsoft/TypeScript · GitHub
Open Graph Title: Rest type parameter in generics for use with intersection types · Issue #3870 · microsoft/TypeScript
X Title: Rest type parameter in generics for use with intersection types · Issue #3870 · microsoft/TypeScript
Description: A common pattern in JavaScript libraries is to copy properties from a variable number of arguments onto the first argument. This is also the behavior of Object.assign. A simple implementation of this pattern in TypeScript might look like...
Open Graph Description: A common pattern in JavaScript libraries is to copy properties from a variable number of arguments onto the first argument. This is also the behavior of Object.assign. A simple implementation of th...
X Description: A common pattern in JavaScript libraries is to copy properties from a variable number of arguments onto the first argument. This is also the behavior of Object.assign. A simple implementation of th...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/3870
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Rest type parameter in generics for use with intersection types","articleBody":"A common pattern in JavaScript libraries is to copy properties from a variable number of arguments onto the first argument. This is also the behavior of `Object.assign`. A simple implementation of this pattern in TypeScript might look like this:\n\n``` ts\nfunction assign(destination: any, ...sources: any[]): any {\n sources.forEach(function (source) {\n for (let key in source) {\n if (source.hasOwnProperty(key)) {\n destination[key] = source[key];\n }\n }\n });\n return destination;\n}\n```\n\nThis works fine, however with intersection types in 1.6 this could be better:\n\n``` ts\nfunction assign\u003cT, U>(destination: T, ...sources: U[]): T & U {\n ...\n}\n```\n\nThis works for one argument, but more than one fail because `U` isn't actually the type needed for the intersection. What is needed (as suggested [here](https://github.com/Microsoft/TypeScript/pull/3622#issuecomment-121686761)) is a rest type parameter. It might look something similar to the following:\n\n``` ts\nfunction assign\u003cT, ...U>(destination: T, ...sources: U[]): T & U {\n```\n\n`U` would be the intersection of all rest parameters passed to `assign()` and the return value would expand to something like `T & (U0 & ... UN)`. With the push for composable types and ES6 standardizing the above behavior (in `Object.assign`) a syntax for generics like this would be very useful.\n","author":{"url":"https://github.com/bryanforbes","@type":"Person","name":"bryanforbes"},"datePublished":"2015-07-15T17:50:28Z","url":"https://github.com/microsoft/TypeScript/issues/3870"}
| 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:cc056173-41d1-35fd-5083-9be0b6a5373a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A45A:3D4705:C426C7:FD8367:6B00ED01 |
| html-safe-nonce | 3ef54f7887a950711a91cb86ea61058d58c850100cb13b102dc0a111c7a38a5c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDVBOjNENDcwNTpDNDI2Qzc6RkQ4MzY3OjZCMDBFRDAxIiwidmlzaXRvcl9pZCI6Ijg1NTU1NzgzNDcyMzU2MzQ0MzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 18799d40e4ee73c6600ea27d767edc9139e3792a410c785f41a91ccad2ca1a18 |
| hovercard-subject-tag | issue:95251645 |
| 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/3870/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a0cc576679d3f4c5436bc317ef5468fb848ca158715ef9ec06b4a23b96c83002/microsoft/TypeScript/issues/3870 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a0cc576679d3f4c5436bc317ef5468fb848ca158715ef9ec06b4a23b96c83002/microsoft/TypeScript/issues/3870 |
| og:image:alt | A common pattern in JavaScript libraries is to copy properties from a variable number of arguments onto the first argument. This is also the behavior of Object.assign. A simple implementation of th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bryanforbes |
| 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