-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Account for discrepancy between soft privacy of private
vs hard privacy of #
#44670
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
Is it feature request or what action you are expecting? If it's feature request - please update issue to match Feature request template. There is warning in docs https://www.typescriptlang.org/docs/handbook/2/classes.html#caveats for |
Thanks @IllusionMH -- I didn't notice this. I searched the docs for If you think that the docs are sufficient in preventing any future confusion with the use of |
@rpivo It might help if you gave an example of an exact situation in code where you expected an error or warning, but the current compiler didn't give one. All three of the "no good" lines in your example already give errors in the current compiler. |
@nmain I think it's more about why the first three error and the last one doesn't. People might approach When I first started looking into this, I figured I think the docs could elaborate on this a bit more than they do. From the docs:
|
They can't, because |
@fatcerberus to be clear, I'm not suggesting typescript updates their |
See also #31670
The language in there is precise and actionable. What more do you think should be said? |
@RyanCavanaugh I think a small example showing different compilation results could be helpful, and maybe even tradeoffs that could be considered (eg, can hard privacy have effects on performance? maybe just statically checked privacy is all that a project needs? what are the escape hatches that TS provides?). I think knowing the difference between hard privacy and soft privacy might not be so obvious to a lot of people. I guess I would expect more people to not know these differences as Maybe more documentation like this would be out of scope for the TS docs. I would think it would be helpful. |
A link in our docs to a well commmented playground showing both and letting folks play with the classes (like we do for types vs interfaces ) would probably solve this quite well. Open to PRs on the website 👍🏻 |
@orta thanks -- I'd be happy to help with this. Will follow up. |
With the upcoming TC39 class fields proposal landing in 2022, there is a soft private vs hard private discrepancy between vanilla JS'
#
and TypeScript'sprivate
. This discrepancy might not be obvious to all users and could cause issues.Maybe the TS docs should highlight this discrepancy, or maybe we should have some kind of warning. Alternatively (and this would have to be raised elsewhere), maybe there should be a TypeScript ESLint warning for this.
TypeScript's
private
is soft private and has escape hatches like the ability to use bracket notation to access a private field.The new
#
private field prefix is hard private and doesn't allow for this. In the example below, TS compilesprivate
and#
differently, and the console log at the bottom shows that accessing these values produces different results.Compiles to:
The text was updated successfully, but these errors were encountered: