Title: Proposal - Class property helper · Issue #9769 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal - Class property helper · Issue #9769 · microsoft/TypeScript
X Title: Proposal - Class property helper · Issue #9769 · microsoft/TypeScript
Description: Scenario Inspired by a conversation in #9733, it is a continual "shock" to many that private doesn't mean private at run-time and is nothing more than a design time only convention. Most of the proposed solutions though require using a c...
Open Graph Description: Scenario Inspired by a conversation in #9733, it is a continual "shock" to many that private doesn't mean private at run-time and is nothing more than a design time only convention. Most of the pro...
X Description: Scenario Inspired by a conversation in #9733, it is a continual "shock" to many that private doesn't mean private at run-time and is nothing more than a design time only convention. M...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/9769
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal - Class property helper","articleBody":"# Scenario\n\nInspired by a conversation in #9733, it is a continual \"shock\" to many that `private` doesn't mean `private` at run-time and is nothing more than a design time only convention. Most of the proposed solutions though require using a closure per instance to make these variables not visible. While I personally prefer a `WeakMap` to not expose such variables, for those using `class` constructs and wanting to constrain the visibility of the properties on the class, using the keyword `private` is obviously far more comfortable.\n\nWhile TC39 are considering [Class Property Declaration](https://github.com/jeffmo/es-class-fields-and-static-properties) there is no current proposal around visibility of properties. That means at run-time `private` will still be a convention for a long long time.\n\nI would say though one of the most surprising thing though is the enumerability of the `private` properties, as class instances could cause challenges at run-time when enumerating of the properties.\n# Proposal\n\nWhen emitting `private` members, add them to the instance using a helper which would define them as non-enumerable. By using a new helper function, it would allow others to potentially change the way the properties are defined and potentially \"future proof\" TypeScript as the proposals of class properties are further refined.\n\nThere are no semantic changes with this proposal. The only changes would be in the emit.\n\nFor example:\n\n``` typescript\nclass Foo {\n private a: string;\n private b = 12;\n protected c: number;\n protected d = false;\n e: string;\n f = 'foo';\n}\n```\n\nCurrently results in a ES5 emit of:\n\n``` js\nvar Foo = (function () {\n function Foo() {\n this.b = 12;\n this.d = false;\n this.f = 'foo';\n }\n return Foo;\n}());\n```\n\nBut would instead result in something like:\n\n``` js\nfunction __defineClassProperties(i, d) {\n var e, pd = {};\n for (var k in d) {\n e = !!(d[k].private || d[k].protected || false);\n pd[k] = { writable: true, configurable: true, enumerable: e };\n if ('value' in d[k]) {\n pd[k].value = d[k].value;\n }\n }\n Object.defineProperties(i, pd);\n}\n\nvar Foo = (function () {\n function Foo() {\n __defineClassProperties(this, {\n a: { private: true },\n b: { private: true, value: 12 },\n c: { protected: true },\n d: { protected: true, value: false },\n e: { },\n f: { value: 'foo' }\n }\n return Foo;\n}());\n```\n\nThe above does not account for getter and setter properties, only because I have spent too much of my morning writing this, but I think the pattern of offloading the definition of class instance properties to a helper function could provide a better level of abstraction for something that may change in the future, allow protected and private members to be non-enumerable and potentially allow other developers to change the behaviour if they want to.\n","author":{"url":"https://github.com/kitsonk","@type":"Person","name":"kitsonk"},"datePublished":"2016-07-16T06:52:59Z","url":"https://github.com/microsoft/TypeScript/issues/9769"}
| 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:8c2ee353-8911-0c39-3cd3-8ad2caab3540 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CC4C:FC5BE:8171FC:ADD851:6B00E092 |
| html-safe-nonce | 626c323823908958e015372452b81ddc4a302d3088aaad329c690351d2bce643 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQzRDOkZDNUJFOjgxNzFGQzpBREQ4NTE6NkIwMEUwOTIiLCJ2aXNpdG9yX2lkIjoiMzUwMjE5MDYxMTA5NTE0Mzg2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | af1bef78739edcde4d340bb05ac13b37afce6ee057aac33ca033fb0b5693a940 |
| hovercard-subject-tag | issue:165914559 |
| 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/9769/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6d84b57225137905a5e86275417028348014408c22dfeb0d8c359457a6ccb4e2/microsoft/TypeScript/issues/9769 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6d84b57225137905a5e86275417028348014408c22dfeb0d8c359457a6ccb4e2/microsoft/TypeScript/issues/9769 |
| og:image:alt | Scenario Inspired by a conversation in #9733, it is a continual "shock" to many that private doesn't mean private at run-time and is nothing more than a design time only convention. Most of the pro... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kitsonk |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width