Title: Represent the types of function parameters that mutate inside the function. · Issue #22865 · microsoft/TypeScript · GitHub
Open Graph Title: Represent the types of function parameters that mutate inside the function. · Issue #22865 · microsoft/TypeScript
X Title: Represent the types of function parameters that mutate inside the function. · Issue #22865 · microsoft/TypeScript
Description: In JavaScript is possible to mutate objects inside functions. Right now, the following code in JavaScript: function merge(x,y) { Object.assign(x,y); } let x = {a: 1}; merge(x, {b: 2}); console.log(x.b); Can't be written in TypeScript wit...
Open Graph Description: In JavaScript is possible to mutate objects inside functions. Right now, the following code in JavaScript: function merge(x,y) { Object.assign(x,y); } let x = {a: 1}; merge(x, {b: 2}); console.log(...
X Description: In JavaScript is possible to mutate objects inside functions. Right now, the following code in JavaScript: function merge(x,y) { Object.assign(x,y); } let x = {a: 1}; merge(x, {b: 2}); console.log(...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/22865
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Represent the types of function parameters that mutate inside the function.","articleBody":"\u003c!-- If you have a SUGGESTION:\r\n Most suggestion reports are duplicates, please search extra hard before logging a new suggestion.\r\n See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md\r\n-->\r\n\r\nIn JavaScript is possible to mutate objects inside functions. Right now, the following code in JavaScript:\r\n\r\n```js\r\nfunction merge(x,y) {\r\n Object.assign(x,y);\r\n} \r\n\r\nlet x = {a: 1}; \r\nmerge(x, {b: 2});\r\nconsole.log(x.b);\r\n```\r\n\r\nCan't be written in TypeScript without casting the type. There are a few options whose type definition is wrong in all scenarios I can think of (maybe I'm missing a better option):\r\n\r\n## Option 1\r\n\r\n```ts\r\nlet x: {a: number, b: number} = {a: 1}; // Error, missing b\r\nmerge(x, {b: 2});\r\n```\r\n\r\n## Option 2\r\n\r\n```ts\r\nlet x: {a: number, b: number} = {a: 1, b: 2};\r\nmerge(x, {b: null});\r\n// From here, x.b is not a number anymore, but you could do\r\nlet y: number= x.b;\r\n```\r\n\r\n## Suggestion\r\n\r\nThere could be an extension to function parameter definition like the following:\r\n\r\n```ts\r\n// then keyword indicates that before it can be type A, and after it will be of type A&B.\r\nfunction merge\u003cA,B>(x: A then x2: A&B, y: B) {\r\n Object.assign(x2,y);\r\n}\r\n\r\nlet x: {a: number, b: number} = {a: 1, b: 2};\r\nmerge(x, {b: null});\r\n// Here, type of x is {a: number, b: number} & {b: null}\r\nx.b; // Type null\r\n```\r\n\r\nThere, we indicate that whatever type was x before, now it is something different. The code above could be written in TypeScript as follows:\r\n\r\n```ts\r\n// then keyword indicates that before it can be type A, and after it will be of type A&B.\r\nfunction merge\u003cA,B>(x: A, y: B) {\r\n Object.assign(x,y);\r\n}\r\n\r\nlet x: {a: number, b: number} = {a: 1, b: 2};\r\nmerge(x, {b: null});\r\n// Here, type of x is {a: number, b: number} & {b: null}\r\nlet xAfterMerge = x as {a: number, b: number} & {b: null};\r\n// Since this line, x should not be used but xAfterMerge\r\nxAfterMerge.b; // Type null\r\n```\r\n\r\n### Another example\r\n\r\n```ts\r\ninterface Before {\r\n address: string;\r\n}\r\n\r\ninterface After {\r\n addr: string;\r\n}\r\n\r\nfunction map(userb: Before then usera: After) {\r\n usera.addr = userb.address;\r\n delete userb.address;\r\n}\r\n\r\nlet u = {adress: \"my street\"};\r\nmap(u);\r\nconsole.log(u.addr);\r\n```\r\n\r\nThat could be syntax sugar for this:\r\n\r\n```ts\r\ninterface Before {\r\n address: string;\r\n}\r\n\r\ninterface After {\r\n addr: string;\r\n}\r\n\r\nfunction map(userb: any) {\r\n userb.addr = userb.address;\r\n delete userb.address;\r\n}\r\n\r\nlet u = {adress: \"my street\"};\r\nmap(u); \r\nconsole.log((u as After).addr);\r\n```\r\n\r\n## Syntax\r\n\r\nIt could be something like:\r\n\r\n```\r\nidentifier: type *then* identifier: type \r\n```\r\n\r\nWith the identifiers being different, and with the types being mandatory an extension of Object. \r\n","author":{"url":"https://github.com/lilezek","@type":"Person","name":"lilezek"},"datePublished":"2018-03-25T15:11:01Z","url":"https://github.com/microsoft/TypeScript/issues/22865"}
| 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:856a785b-97b0-afea-f97c-63cef04b1872 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9902:2679E9:2C8A829:3B7AC57:6B01D453 |
| html-safe-nonce | c95cb9a0b078273668cf158bb34d582e13e1d7ef42137cba8a2ae2a3655389b1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTAyOjI2NzlFOToyQzhBODI5OjNCN0FDNTc6NkIwMUQ0NTMiLCJ2aXNpdG9yX2lkIjoiNzEyMjUwODAwMTQ5NTM0MDExNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bafb3a22f8b7b7b90b688d01b3479f00b2f659e4e7b5dd922e92db4d44652a1d |
| hovercard-subject-tag | issue:308357432 |
| 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/22865/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6a3d2a41e75d24d37c6b7da0df59dcc2964852ad749674bd0dca3812821951fc/microsoft/TypeScript/issues/22865 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6a3d2a41e75d24d37c6b7da0df59dcc2964852ad749674bd0dca3812821951fc/microsoft/TypeScript/issues/22865 |
| og:image:alt | In JavaScript is possible to mutate objects inside functions. Right now, the following code in JavaScript: function merge(x,y) { Object.assign(x,y); } let x = {a: 1}; merge(x, {b: 2}); console.log(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lilezek |
| 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