Title: Suggestion: Use the keyword "readonly" as an accessor for classes · Issue #10064 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Use the keyword "readonly" as an accessor for classes · Issue #10064 · microsoft/TypeScript
X Title: Suggestion: Use the keyword "readonly" as an accessor for classes · Issue #10064 · microsoft/TypeScript
Description: Use case: we need a class Counter which provides a property count that is read from outside the class and written from inside the class. In JavaScript, if we want to use a class instead of a closure, we have to make a public property: /*...
Open Graph Description: Use case: we need a class Counter which provides a property count that is read from outside the class and written from inside the class. In JavaScript, if we want to use a class instead of a closur...
X Description: Use case: we need a class Counter which provides a property count that is read from outside the class and written from inside the class. In JavaScript, if we want to use a class instead of a closur...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/10064
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Use the keyword \"readonly\" as an accessor for classes","articleBody":"Use case: we need a class `Counter` which provides a property `count` that is read from outside the class and written from inside the class.\n\nIn JavaScript, if we want to use a class instead of a closure, we have to make a public property:\n\n```\n/**\n * Read-only access to the property \"count\", please\n */\nclass Counter {\n constructor() {\n this.count = 0\n }\n inc() {\n ++this.count\n }\n}\nlet counter = new Counter()\ncounter.inc()\nconsole.log(counter.count) // 1\ncounter.count = 10 // No error but it doesn't respect the JSDoc\n```\n\nIn TypeScript, we could proceed in the same way. But it is tempting to use an accessor `private`:\n\n```\nclass Counter {\n private count = 0\n public inc() {\n ++this.count\n }\n public getCount() {\n return this.count\n }\n}\nlet counter = new Counter()\ncounter.inc()\nconsole.log(counter.getCount()) // 1\ncounter.count = 10 // Error: Property 'count' is private and only accessible within class 'Counter'\n```\n\nHowever, it is verbose, less readable, less performant .\n\nI suggest the following syntax:\n\n```\nclass Counter {\n readonly count = 0\n public inc() {\n ++this.count\n }\n}\nlet counter = new Counter()\ncounter.inc()\nconsole.log(counter.count) // 1\ncounter.count = 10 // Error: Property 'count' is readonly and can be only mutated within class 'Counter'\n```\n\nIt is the same solution as in JavaScript, but with a better control to access (and less JSDoc).\n","author":{"url":"https://github.com/paleo","@type":"Person","name":"paleo"},"datePublished":"2016-08-01T07:41:36Z","url":"https://github.com/microsoft/TypeScript/issues/10064"}
| 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:f25e91b5-c526-f4da-3115-dfc474b29a3e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 87E6:1E070F:685C89:8AF99F:6AFBCB59 |
| html-safe-nonce | f524963835f6604edc8cfbd094546b3b99a06a4c5b54863968e610a9add516cd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4N0U2OjFFMDcwRjo2ODVDODk6OEFGOTlGOjZBRkJDQjU5IiwidmlzaXRvcl9pZCI6IjYyMDQ2MTI3MzM3MzIzNzUzODUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | eb5baa429fefefb0fb9372f83dd35cfc5758a2af2b6553e6dcbf84ed8c987e12 |
| hovercard-subject-tag | issue:168585650 |
| 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/10064/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4b5a213a7c592393aed4fc7ec1aa84b2415761c0c51fe25dd89fda3a030c8e61/microsoft/TypeScript/issues/10064 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4b5a213a7c592393aed4fc7ec1aa84b2415761c0c51fe25dd89fda3a030c8e61/microsoft/TypeScript/issues/10064 |
| og:image:alt | Use case: we need a class Counter which provides a property count that is read from outside the class and written from inside the class. In JavaScript, if we want to use a class instead of a closur... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | paleo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 18742cb872ec73544356e60359266b67aafece70c4ce008042e06227924cdd07 |
| 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 | 820be804f18e1ef3940bd74a00d41d73d02e2b91 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width