Style guide: Leading underscore conflict with TypeScript docs
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[ ] Performance issue
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:
Current behavior
The style guide states:
Avoid prefixing private properties and methods with an underscore.
https://angular.io/guide/styleguide#style-03-04
This is in conflict with how TypeScript documentation suggests we create getters and setters, and the behavior of both WebStorm and the prominent VSC extension when autogenerating getters and setters.
The pattern is like:
export class Foo {
private _bar: string;
...
public get bar(): string { return this._bar; }
Ref:
https://www.typescriptlang.org/docs/handbook/classes.html#accessors
https://github.com/johnpapa/angular-styleguide/issues/861
Expected behavior
Style guide should either provide a recommendation on preferred alternative or remove this guideline.
Preferred alternative should be lintable, by e.g. tslint-consistent-codestyle.naming-convention (should be a universal rule for private fields and methods).
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
Our organization is put in friction because we are required to encapsulate, required to follow TS documentation, and required to follow the style guide.
Environment
Angular version: N/A
Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: XX
- Platform:
Others:
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:20 (4 by maintainers)
Top GitHub Comments
Adding a new rule to my tslint.config fixed to me:
I’m using PHPStorm and the “no-underscore” rule makes no sense to me. It’s much easier and more efficient to use an leading underscore. Adding prefixes like “get” or “set” to getter/setters makes the code ugly. That’s not Java, it’s Typescript.
Furthermore the angular guidelines describes only that underscores shouldn’t be used but explain nothing about any alternative when using getter/setters. Renaming the getter/setters manually is not an option. https://angular.io/guide/styleguide#style-03-04
@pburkindine You should avoid prefixing every private attribute with an underscrore. In your case it’s the best solution to the problem.
IMO Avoid doesn’t mean forbidden.