-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Shorthand restricting accessor accessibility. (get; private set; inspired by C#) #48362
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
Duplicate of #37487 |
You forgot to fill out the issue template for feature requests. It contains a "viability checklist" which pretty much rules your suggestion out.
|
C# recommends everyone use property getters/setters because you incur a binary API break when changing a field to an accessor pair, but this is a much much smaller problem in JavaScript -- wrapping a bare field with a get/set pair is not something you should be doing regularly, so there's no syntax in JS or TS for this. |
@RyanCavanaugh The main motivation here seems to be less about the property shorthand and more "I want to have a property that can only be written from inside the class" |
True; I guess my comment applies more to the non-#37487 aspects of this |
Yes, it's about
Sounds very similar. Why doesn't TypeScript use the |
When reading keywords I would assume |
Suggestion
🔍 Search Terms
private set
shorthand accessor
⭐ Suggestion
In C# you can do this:
to reach this in TypeScript you have to write a short novel:
This works great. But is a lot of code for such a tiny thing.
Write less code with less risk of errors. Otherwise you have to create a mirrored variable with e.g. leading
_
underscore. And write this stupid short code. It's harder to read. You write the name and types over and over again. And possible make a mistake. But anyway, it would great to short up this by C# inspired syntax or a new keyword. Something betweenprivate
andpublic
. A keyword forprivate set; public get
.But I'm not sure how TypeScript will handle this. I think code needs to be added to JavaScript during compilation. But how to avoid collisions? I mean just adding
_{variableName}
is maybe not the best idea. It must be added to a hidden layer. ... Anyway, this is just a suggestion hoping you have a better idea than me.The syntax can be similar to C# or a new keyword. It world be create to have a short way to make a property readonly for public and writeable in private scope. In best case with an initial value.
The text was updated successfully, but these errors were encountered: