Title: Support proposed ES Rest/Spread properties · Issue #2103 · microsoft/TypeScript · GitHub
Open Graph Title: Support proposed ES Rest/Spread properties · Issue #2103 · microsoft/TypeScript
X Title: Support proposed ES Rest/Spread properties · Issue #2103 · microsoft/TypeScript
Description: es7 proposal : https://github.com/sebmarkbage/ecmascript-rest-spread Spread properties Typing In my opinion the goal of this method is to be able to duplicate an object and changing some props, so I think it's particularly important in t...
Open Graph Description: es7 proposal : https://github.com/sebmarkbage/ecmascript-rest-spread Spread properties Typing In my opinion the goal of this method is to be able to duplicate an object and changing some props, so ...
X Description: es7 proposal : https://github.com/sebmarkbage/ecmascript-rest-spread Spread properties Typing In my opinion the goal of this method is to be able to duplicate an object and changing some props, so ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/2103
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support proposed ES Rest/Spread properties","articleBody":"es7 proposal : https://github.com/sebmarkbage/ecmascript-rest-spread\n## Spread properties\n### Typing\n\nIn my opinion the goal of this method is to be able to duplicate an object and changing some props, so I think it's particularly important in this case to not check duplicate property declaration : \n\n``` typescript\nvar obj = { x: 1, y: 2};\nvar obj1 = {...obj, z: 3, y: 4}; // not an error\n```\n\nI have a very naive [type check algorithm](https://github.com/fdecampredon/jsx-typescript/blob/3c692cbb80622b74d1b308b3c5dcab6b993b4111/src/compiler/checker.ts#L5665-L5671) for a similar feature (`JSXSpreadAttribute`) in my little `jsx-typescript` fork: I just copy the properties of the _spread object_ in the properties table when I encounter a spread object, and override those property if I encounter a declaration with a similar name.\n### Emitting\n\n[jstransform](https://github.com/facebook/jstransform/) use `Object.assign`, [babel](https://babeljs.io/) introduce a shim: \n\n``` javascript\nvar _extends = Object.assign || function (target) { for (var i = 1; i \u003c arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };\n```\n\nWe could either force the presence of `assign` function on `ObjectConstructor` interface, or provide a similar function (with a different name).\n\nI think that the optimal solution would be to not emit any helper in `es6` target (or if `Object.assign` is defined), and to emit an helper function for `es5`, `es3`.\n\n``` javascript\nvar obj = { x: 1, y: 2};\nvar obj1 = {...obj, z: 3};\n\n/// ES6 emit\nvar obj = {x: 1, y: 2};\nvar obj1= Object.assign({}, obj, { z: 3 });\n\n//ES3 emit\nvar __assign = function (target) { \n for (var i = 1; i \u003c arguments.length; i++) { \n var source = arguments[i]; \n for (var key in source) { \n if (Object.prototype.hasOwnProperty.call(source, key)) { \n target[key] = source[key];\n } \n } \n } \n return target; \n};\n\nvar obj = {x: 1, y: 2};\nvar obj1= __assign({}, obj, { z: 3 });\n```\n## Rest properties\n### Typing\n\nFor simple object the new type is a subtype of the assignation that does not contains properties that has been captured before the rest properties : \n\n``` typescript\nvar obj = {x:1, y: 1, z: 1};\nvar {z, ...obj1} = obj;\nobj1// {x: number; y:number};\n```\n\nIf the destructuring assignment has an index declaration, the result has also a similar index declaration: \n\n``` typescript\nvar obj: { [string: string]: string };\nvar {[excludedId], ...obj1} = obj;\nobj1// { [string: string]: string };\n```\n\nnew/call declarations are obviously not captured:\n\n``` typescript\nvar obj: { (): void; property: string};\nvar { ...obj1} = obj;\nobj1// { property: string };\n```\n### Emitting\n\nIt is not possible to emit _rest properties_ without an helper function, this one is from babel: \n\n``` typescript\nvar obj = {x:1, y: 1, z: 1};\nvar {z, ...obj1} = obj;\n```\n\n``` javascript\nvar __objectWithoutProperties = function(obj, keys) {\n var target = {};\n for (var i in obj) {\n if (keys.indexOf(i) \u003e= 0) continue;\n if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;\n target[i] = obj[i];\n }\n return target;\n};\n\nvar obj = {x:1, y: 1, z: 1};\nvar z = obj.z;\nvar obj1 = __objectWithoutProperties(obj, [\"z\"]);\n```\n\n_Edit: added some little typing/emitting example_\n","author":{"url":"https://github.com/fdecampredon","@type":"Person","name":"fdecampredon"},"datePublished":"2015-02-21T21:31:32.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":96},"url":"https://github.com/2103/TypeScript/issues/2103"}
| 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:05d9155a-226c-9dff-9333-13af6e96e919 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B774:45C3A:ADB37:EDCA4:6A4D515F |
| html-safe-nonce | b3505dba0adb203ef0acf6810ad8c1d0f6d1ffc9c76da1e6a3557128e05b6776 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzc0OjQ1QzNBOkFEQjM3OkVEQ0E0OjZBNEQ1MTVGIiwidmlzaXRvcl9pZCI6IjU2NjY2MjIyODU4MjY4Mjk3NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 87975e66bc09c05cf697a359fb17835fb905d26e6390cae5e04d5547a344a50f |
| hovercard-subject-tag | issue:58475863 |
| 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/2103/issue_layout |
| twitter:image | https://opengraph.githubassets.com/49b5ee41041016289001c1b1ae65f445b587d931dd84f2775f6053b7a92454d7/microsoft/TypeScript/issues/2103 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/49b5ee41041016289001c1b1ae65f445b587d931dd84f2775f6053b7a92454d7/microsoft/TypeScript/issues/2103 |
| og:image:alt | es7 proposal : https://github.com/sebmarkbage/ecmascript-rest-spread Spread properties Typing In my opinion the goal of this method is to be able to duplicate an object and changing some props, so ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fdecampredon |
| hostname | github.com |
| expected-hostname | github.com |
| None | 92571a8944142227b7e19cd10918b1ddd06e5066c1ad5bc7e4769cf6140a87e6 |
| 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 | 56fc8347865a14e2ec811533d68f929cf4e0ec19 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width