-
Notifications
You must be signed in to change notification settings - Fork 12.8k
feature request: runtime privacy #31914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm sure someone will write something that trivializes this, whether in a quick fix or elsewhere. |
Part of the reason that
Can you elaborate on this? I understand migration costs (the next point you make), but we can make that easier with refactorings. Why would you want |
tc39/proposal-class-fields#100 It is the most disliked thing to land in JS, looks like.
It doesn't have to be meant that way, and because it is exclusive to TS, it would be fine to do it a new way (that doesn't break existing TS code). |
deleted, there's lots of reasons why people don't like |
The technical justifications behind |
It is sad for both sides. The TypeScript could use |
Search Terms
runtime privacy
Suggestion
It'd be great there was a compiler option to enable runtime privacy.
When this mode is enabled, the compiled output would use simple WeakMap-based privates.
Alternatively, a builtin decorator could turn it on/off on a per/class basis.
Use Cases
It happens often that someone writes a library in TypeScript, but the end user consumes the library in plain JavaScript without the intended privacy.
Examples
Nothing changes syntactically. For example:
consumer code:
Checklist
My suggestion meets these guidelines:
More
There are already private fields with
#
that are potentially coming to the language, but many people won't want to use it overprivate
syntax, plus many source codes already useprivate
and it may be inconvenient to convert them all over once#
lands.An option would be great, and the implementation can be light-weight, with a simple WeakMap implementation as follows:
The above snippet could compile to something like the following, using a single WeakMap per module scope no matter how many classes are inside the file (unlike Babel's which uses one WeakMap per private field which is a bit excessive):
consumer code:
That's the simplest implementation with clean class-like output.
Here's another one that makes one method per class, instead of one per instance:
The text was updated successfully, but these errors were encountered: