Title: Readonly methods and readonly types · Issue #61690 · microsoft/TypeScript · GitHub
Open Graph Title: Readonly methods and readonly types · Issue #61690 · microsoft/TypeScript
X Title: Readonly methods and readonly types · Issue #61690 · microsoft/TypeScript
Description: 🔍 Search Terms readonly methods ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existing JavaScript code This could be implemented without emitt...
Open Graph Description: 🔍 Search Terms readonly methods ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existing JavaScript code...
X Description: 🔍 Search Terms readonly methods ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existing JavaScr...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/61690
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Readonly methods and readonly types","articleBody":"### 🔍 Search Terms\n\nreadonly methods\n\n### ✅ Viability Checklist\n\n- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\n- [x] This wouldn't change the runtime behavior of existing JavaScript code\n- [x] This could be implemented without emitting different JS based on the types of the expressions\n- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\n- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types\n- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals\n\n### ⭐ Suggestion\n\nI suggest using the readonly **keyword** for methods, indicating that they don't change the contents of the \"this\" object, but only receive data. Similar to [const methods](https://isocpp.org/wiki/faq/const-correctness#const-member-fns) in C++. This will provide good security from possible errors, as well as better understanding of the code:\n```ts\nclass A {\n private value = 10;\n\n getValue() readonly { // mark the method as readonly, prohibiting changing the contents of the object \n this.value = 0; // Error: can't modify readonly object\n this.setValue(0); // Error: can't call a mutating method on a readonly object\n return this.value;\n }\n setValue(val : number) {\n this.value = val; // OK\n }\n}\n```\n\nAnd the types themselves can also be marked as **readonly**, meaning that all their contents become readonly, and only readonly methods are available:\n```ts\nfunction foo(a : readonly A) {\n a.setValue(0); // Error: can't call a mutating method on a readonly object\n a.getValue(); // OK\n} \n```\n\nWe already have such syntax for declaring **readonly arrays**. And it will become a universal declaration for any readonly objects.\n\nAnd if the method does not change the contents of the class, but returns a reference to some internal object of the class, then we can overload the signature of this method in two versions (with and without readonly):\n```ts\n getData() readonly : readonly number[];\n getData() : number[];\n```\n\nIt is also necessary to clarify that the readonly type means **deep readonly** of the entire nested structure of the object, not just the first-level properties. And if we need a different behavior for individual properties, we can overload the getters for them, just like in the example above.\n\nAs an additional option, we can use **readonly** for the declared interface or class as a whole, eliminating the need to write it for each method:\n```ts\ninterface ReadonlySome readonly { // mark the interface as readonly\n getValue() : number; // becomes a readonly method\n calculate(data: readonly number[]) : number; // becomes a readonly method\n}\n```\n\n\n### 📃 Motivating Example\n\n-\n\n### 💻 Use Cases\n\nAs I mentioned above, this concept is implemented in C++, but using the **const** keyword:\n```cpp\nclass A {\n int _value = 10;\n\n int getValue() const { \n _value = 0; // Error\n setValue(0); // Error\n return _value;\n }\n void setValue(int val) {\n _value = val; // OK\n }\n foo(const A& a) {\n a.setValue(0); // Error\n }\n};\n```\n\nIt is also partially implemented in C#, but in a very limited version. I don't see any reason why it couldn't be fully implemented.\n\nIn TypeScript as an alternative we can try to wrap \"this\" in some kind of typing:\n```ts\nclass A {\n readonlyMethod(this : SomeReadonlyWrap\u003cA>) { }\n}\n```\nbut I haven't found the right implementation of this wrapper yet. And anyway, it's very cumbersome to write it like that for all methods, especially in template classes.\n\n","author":{"url":"https://github.com/Perfectoff","@type":"Person","name":"Perfectoff"},"datePublished":"2025-05-12T05:27:24Z","url":"https://github.com/microsoft/TypeScript/issues/61690"}
| 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:a1b7db88-a1d7-8af0-62ea-68d24be50d71 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8074:22EC8D:1E198E:286DCE:6AFBE11C |
| html-safe-nonce | 9a07a3559c8ba711fe7439d801c4821bdaef7e01394279938be46893032099cb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDc0OjIyRUM4RDoxRTE5OEU6Mjg2RENFOjZBRkJFMTFDIiwidmlzaXRvcl9pZCI6IjE1Nzg2NTU1NTI1OTI2NjY5MDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a35d891bfaaab3b6f83d7dfb6a9aac48143e6755bf7069e21f94ab0a41c91323 |
| hovercard-subject-tag | issue:3055675499 |
| 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/61690/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8b02381acd21cb8fe0f491c6f21d48ffac3fe68d9a6244c111b963b432d7ebdd/microsoft/TypeScript/issues/61690 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8b02381acd21cb8fe0f491c6f21d48ffac3fe68d9a6244c111b963b432d7ebdd/microsoft/TypeScript/issues/61690 |
| og:image:alt | 🔍 Search Terms readonly methods ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existing JavaScript code... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Perfectoff |
| hostname | github.com |
| expected-hostname | github.com |
| None | 744d4381b1c4dbd6d8736c7d7e5bbe7dc11217584e457a3555020c631764e8ec |
| 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 | df859c48a4c8b4c0009c19bb4e82101aacb4b998 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width