Title: Add spread/rest higher-order types operator · Issue #10727 · microsoft/TypeScript · GitHub
Open Graph Title: Add spread/rest higher-order types operator · Issue #10727 · microsoft/TypeScript
X Title: Add spread/rest higher-order types operator · Issue #10727 · microsoft/TypeScript
Description: The spread type is a new type operator that types the TC39 stage 3 object spread operator. Its counterpart, the difference type, will type the proposed object rest destructuring operator. The spread type { ...A, ...B } combines the prope...
Open Graph Description: The spread type is a new type operator that types the TC39 stage 3 object spread operator. Its counterpart, the difference type, will type the proposed object rest destructuring operator. The sprea...
X Description: The spread type is a new type operator that types the TC39 stage 3 object spread operator. Its counterpart, the difference type, will type the proposed object rest destructuring operator. The sprea...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/10727
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add spread/rest higher-order types operator","articleBody":"The spread type is a new type operator that types the [TC39 stage 3 object spread operator](https://github.com/sebmarkbage/ecmascript-rest-spread). Its counterpart, the difference type, will type the proposed object rest destructuring operator. The spread type `{ ...A, ...B }` combines the properties, but not the call or construct signatures, of entities A and B.\n\nThe pull request is at #11150. The original issue for spread/rest types is #2103. Note that this proposal deviates from the specification by keeping all properties except methods, not just own enumerable ones.\n## Proposal syntax\n\nThe type syntax in this proposal differs from the type syntax as implemented in order to treat spread as a binary operator. Three rules are needed to convert the `{ ...spread1, ...spread2 }` syntax to binary syntax `spread1 ... spread2`.\n1. `{ ...spread }` becomes `{} ... spread`.\n2. `{ a, b, c, ...d}` becomes `{a, b, c} ... d`\n3. Multiple spreads inside an object literal are treated as sequences of binary spreads: `{ a, b, c, ...d, ...e, f, g}` becomes `{a, b, c} ... d ... e ... { f, g }`.\n## Type Relationships\n- Identity: `A ... A ... A` is equivalent to `A ... A` and `A ... A` is equivalent to `{} ... A`.\n- Commutativity: `A ... B` is _not_ equivalent to `B ... A`. Properties of `B` overwrite properties of `A` with the same name in `A ... B`.\n- Associativity: `(A ... B) ... C` is equivalent to `A ... (B ... C)`. `...` is right-associative.\n- Distributivity: Spread is distributive over `|`, so `A ... (B | C)` is equivalent to `A ... B | A ... C`.\n## Assignment compatibility\n- `A ... B` is assignable to `X` if the properties and index signatures of `A ... B` are assignable to those of `X`, and `X` has no call or construct signatures.\n- `X` is assignable to `A ... B` if the properties and index signatures of `X` are assignable to those of `A ... B`.\n### Type parameters\n\nA spread type containing type parameters is assignable to another spread type if the type if the source and target types are both of the form `T ... { some, object, type }` and both source and target have the same type parameter and the source object type is assignable to the target object type.\n### Type inference\n\nSpread types are not type inference targets.\n## Properties and index signatures\n\nIn the following definitions, 'property' means either a property or a get accessor.\n\nThe type `A ... B` has a property `P` if \n1. `A` has a property `P` or `B` has a property `P`, and\n2. Either `A.P` or `B.P` is not a method. \n\nIn this case `(A ... B).P` has the type \n1. Of `B.P` if `B.P` is not optional.\n2. Of `A.P | B.P` if `B.P` is optional and `A` has a property `P`.\n3. Of `A.P` otherwise.\n\n`private`, `protected` and `readonly` behave the same way as optionality except that if `A.P` or `B.P` is `private`, `protected` or `readonly`, then `(A ...B).P` is `private`, `protected` or `readonly`, respectively.\n## Index signatures\n\nThe type `A ... B` has an index signature if `A` has an index signature and `B` has an index signature. The index signature's type is the union of the two index signatures' types.\n## Call and Construct signatures\n\n`A ... B` has no call signatures and no construct signatures, since these are not properties.\n## Precedence\n\nPrecedence of `...` is higher than `\u0026` and `|`. Since the language syntax is that of object type literals, precedence doesn't matter since the braces act as boundaries of the spread type.\n## Examples\n\nTaken from the [TC39 proposal](https://github.com/sebmarkbage/ecmascript-rest-spread/blob/master/Spread.md) and given types.\n### Shallow Clone (excluding prototype)\n\n``` ts\nlet aClone: { ...A } = { ...a };\n```\n### Merging Two Objects\n\n``` ts\nlet ab: { ...A, ...B } = { ...a, ...b };\n```\n### Overriding Properties\n\n``` ts\nlet aWithOverrides: { ...A, x: number, y: number } = { ...a, x: 1, y: 2 };\n// equivalent to\nlet aWithOverrides: { ...A, ...{ x: number, y: number } } = { ...a, ...{ x: 1, y: 2 } };\n```\n### Default Properties\n\n``` ts\nlet aWithDefaults: { x: number, y: number, ...A } = { x: 1, y: 2, ...a };\n```\n### Multiple Merges\n\n``` ts\n// Note: getters on a are executed twice\nlet xyWithAandB: { x: number, ...A, y: number, ...B, ...A } = { x: 1, ...a, y: 2, ...b, ...a };\n// equivalent to\nlet xyWithAandB: { x: number, y: number, ...B, ...A } = { x: 1, ...a, y: 2, ...b, ...a };\n```\n### Getters on the Object Initializer\n\n``` ts\n// Does not throw because .x isn't evaluated yet. It's defined.\nlet aWithXGetter: { ...A, x: never } = { ...a, get x() { throw new Error('not thrown yet') } };\n```\n### Getters in the Spread Object\n\n``` ts\n// Throws because the .x property of the inner object is evaluated when the\n// property value is copied over to the surrounding object initializer.\nlet runtimeError: { ...A, x: never } = { ...a, ...{ get x() { throw new Error('thrown now') } } };\n```\n### Setters Are Not Executed When They're Redefined\n\n``` ts\nlet z: { x: number } = { set x() { throw new Error(); }, ...{ x: 1 } }; // No error\n```\n### Null/Undefined Are Ignored\n\n``` ts\nlet emptyObject: {} = { ...null, ...undefined }; // no runtime error\n```\n### Updating Deep Immutable Object\n\n``` ts\nlet newVersion: { ...A, name: string, address: { address, zipCode: string }, items: { title: string }[] } = {\n ...previousVersion,\n name: 'New Name', // Override the name property\n address: { ...previousVersion.address, zipCode: '99999' } // Update nested zip code\n items: [...previousVersion.items, { title: 'New Item' }] // Add an item to the list of items\n};\n```\n\nNote: If `A = { name: string, address: { address, zipCode: string }, items: { title: string }[] }`, then the type of newVersion is equivalent to `A`\n# Rest types\n\nThe difference type is the opposite of the spread type. It types the TC39 stage 3 object-rest destructuring operator. The difference type `rest(T, a, b, c)` represents the type `T` after the properties `a`, `b` and `c` have been removed, as well as call signatures and construct signatures. \n\nA short example illustrates the way this type is used:\n\n``` ts\n/** JavaScript version */\nfunction removeX(o) {\n let { x, ...rest } = o;\n return rest;\n}\n\n/** Typescript version */\nfunction removeX\u003cT extends { x: number, y: number }\u003e(o: T): rest(T, x) {\n let { x, ...rest }: T = o;\n return rest;\n}\n```\n## Type Relationships\n- `rest(A)` is not equivalent to `A` because it is missing call and construct signatures. \n- `rest(rest(A))` is equivalent to `rest(A)`. \n- `rest(rest(A, a), b)` is equivalent to `rest(rest(A, b), a)` and `rest(A, a, b)`.\n- `rest(A | B, a)` is equivalent to `rest(A, a) | rest(B, a)`.\n## Assignment compatibility\n- `rest(T, x)` is not assignable to `T`.\n- `T` is assignable to `rest(T, x)` because `T` has more properties and signatures.\n## Properties and index signatures\n\nThe type `rest(A, P)` removes `P` from `A` if it exists. Otherwise, it does nothing.\n## Call and Construct signatures\n\n`rest(A)` does not have call or construct signatures.\n## Precedence\n\nDifference types have similar precedence to `-` in the expression grammar, particularly compared to `\u0026` and `|`. TODO: Find out what this precedence is.\n","author":{"url":"https://github.com/sandersn","@type":"Person","name":"sandersn"},"datePublished":"2016-09-06T18:00:37.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":86},"url":"https://github.com/10727/TypeScript/issues/10727"}
| 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:1802aae4-2d0e-c8f8-421e-3536749b6564 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE84:34E523:3CDE23:528ACA:6A4D39A9 |
| html-safe-nonce | 45816467852e856507347a3ebdc5c9922eeef8b1622a2b2cc294b37ff0effbbf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTg0OjM0RTUyMzozQ0RFMjM6NTI4QUNBOjZBNEQzOUE5IiwidmlzaXRvcl9pZCI6Ijg2MjYwMDg1MTM3Mjg4MjE2NzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6e23d51789ce7d2f5d3f5f44c3be3c68ecd7b0537795fc9a139e9a7d5bc648a2 |
| hovercard-subject-tag | issue:175311249 |
| 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/10727/issue_layout |
| twitter:image | https://opengraph.githubassets.com/49bfc68f3021227273005a4ae263b08fa3d73c1595771a03499a085369053a28/microsoft/TypeScript/issues/10727 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/49bfc68f3021227273005a4ae263b08fa3d73c1595771a03499a085369053a28/microsoft/TypeScript/issues/10727 |
| og:image:alt | The spread type is a new type operator that types the TC39 stage 3 object spread operator. Its counterpart, the difference type, will type the proposed object rest destructuring operator. The sprea... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sandersn |
| 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