Query: What is the ideal approach for naming private properties in Typescript and should one always create getters and setters for those properties?
After perusing this link, I found myself reconsidering what I previously thought to be a good coding practice in Typescript: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines
While I acknowledge that these guidelines are not absolute rules, the suggested convention for naming private properties raises concerns regarding proper encapsulation and utilization of private properties in Typescript. I have been accustomed to prefixing private properties with an underscore and creating corresponding getters and setters to access or modify the value of that property. This convention, reminiscent of Anders' Delphi days, helped distinguish it as a property rather than just another variable, enhancing tooling support.
For instance:
...
private _progress: IImportInfo[];
...
get progress(): IImportInfo[] {
return this._progress;
}
set progress(value: IImportInfo[]) {
this._progress = value;
}
...
These values can then be accessed in an html file like so:
<li *ngFor="let item of progress">
Although direct access to the private variable from external sources is possible, it compromises the expected encapsulation benefits typically associated with private variables. Despite this, adhering to encapsulation principles still holds value (in my opinion). However, the purpose of raising this question is not solely based on personal views. Instead, I am seeking insights from experienced angular/typescript developers on best practices for handling Typescript private properties. Even better would be a credible source or website outlining recommended practices for managing private properties.
Appreciate any assistance provided on this matter.