Title: Feature Request: Readonly
Open Graph Title: Feature Request: Readonly
X Title: Feature Request: Readonly
Description: Search Terms readonly, as const, Readonly, Suggestion Readonly to work the way Readonly
Open Graph Description: Search Terms readonly, as const, Readonly, Suggestion Readonly to work the way Readonly
X Description: Search Terms readonly, as const, Readonly, Suggestion Readonly to work the way Readonly<T[]> does without having to create something like ReadonlyArray, ReadonlySet, etc... I would love it if...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/35313
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Feature Request: Readonly\u003cT> should remove mutable methods","articleBody":"## Search Terms\r\n\r\nreadonly, as const, Readonly\u003cT>,\r\n\r\n## Suggestion\r\n\r\nReadonly\u003cT> to work the way Readonly\u003cT[]> does without having to create something like ReadonlyArray\u003cT>, ReadonlySet\u003cT>, etc...\r\n\r\n\r\nI would love it if the `Readonly\u003cT>` mapped type could exclude any method marked as `readonly` or maybe `as cosnt` or something similar.\r\n\r\n## Use Cases\r\n\r\nAt the moment I have to do this:\r\n\r\n```typescript\r\nclass Vector2 {\r\n public constructor(\r\n public x: number,\r\n public x: number,\r\n ) {}\r\n\r\n public add(other: ReadonlyVector2): Vector2 {\r\n return this.clone().addInPlace(other);\r\n }\r\n\r\n public addInPlace(other: ReadonlyVector2): this {\r\n this.x += other.x;\r\n this.y += other.y;\r\n return this;\r\n }\r\n\r\n public clone(): Vector2 {\r\n return new Vector2(this.x, this.y);\r\n }\r\n}\r\n\r\ninterface ReadonlyVector2 {\r\n readonly x: number;\r\n readonly y: number;\r\n add(other: ReadonlyVector2): Vector2;\r\n clone(): Vector2;\r\n}\r\n```\r\n\r\nThis gets very boring with a lot of types, also it's error prone. I could forget to add something to the interface, or worse I could accidentally add something that is mutable.\r\n\r\nI can currently do `method(this: Readonly\u003cthis>) {...}`, which is helpful, but doesn't stop me calling mutable methods on the type.\r\n\r\nInside a marked method you would be unable to mutate the state of member variables and you would unable to call other unmarked methods.\r\n\r\nThis would also allow you to remove the special case of `Readonly\u003cT[]>`. The mutable methods of arrays could be marked with whatever syntax is decided and then `Readonly\u003cT[]>` would just work like any other `Readonly\u003cT>`. Currently I don't bother using `Readonly\u003cT>` since it doesn't really help with anything except C style POD types. \r\n\r\n## Examples\r\n\r\n```typescript\r\nclass Vector2 {\r\n public constructor(\r\n public x: number,\r\n public x: number,\r\n ) {}\r\n\r\n public readonly add(other: Readonly\u003cVector2>): Vector2 {\r\n return this.clone().addInPlace(other);\r\n }\r\n\r\n public addInPlace(other: Readonly\u003cVector2>): this {\r\n this.x += other.x;\r\n this.y += other.y;\r\n return this;\r\n }\r\n\r\n public const clone(): Vector2 {\r\n return new Vector2(this.x, this.y);\r\n }\r\n}\r\n```\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n","author":{"url":"https://github.com/rope-hmg","@type":"Person","name":"rope-hmg"},"datePublished":"2019-11-23T18:04:20Z","url":"https://github.com/microsoft/TypeScript/issues/35313"}
| 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:8cffb810-e120-e7ff-70cf-dfcdd72b021a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA84:BD02F:F01E14:14B73BE:6AFBF55E |
| html-safe-nonce | a50dd6928c5778c2350bf246a9c3f8d23880da589b8e8f8ed04829e09a925fd2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTg0OkJEMDJGOkYwMUUxNDoxNEI3M0JFOjZBRkJGNTVFIiwidmlzaXRvcl9pZCI6IjE3OTY4MzM4NjEwOTUzODg1MTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3abae34056641dc05e64d0a8768d59754aa0a5327fefa1918cc29bbc7be39275 |
| hovercard-subject-tag | issue:527588654 |
| 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/35313/issue_layout |
| twitter:image | https://opengraph.githubassets.com/94ed104201585980fd2684bcc389a536b05ddb246c916e030f084f78928f22cf/microsoft/TypeScript/issues/35313 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/94ed104201585980fd2684bcc389a536b05ddb246c916e030f084f78928f22cf/microsoft/TypeScript/issues/35313 |
| og:image:alt | Search Terms readonly, as const, Readonly, Suggestion Readonly to work the way Readonly |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rope-hmg |
| hostname | github.com |
| expected-hostname | github.com |
| None | fa74413a1d84dac536fcca0740780bcbd3207bafec0e51873b73ff8e8cca032f |
| 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 | b3b5633a74535d9c23385512d534c66c8fde1907 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width