question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Style guide: Leading underscore conflict with TypeScript docs

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
julianpoempcommented, Feb 17, 2019

Adding a new rule to my tslint.config fixed to me:

"variable-name": [
  true,
  "allow-leading-underscore"
]

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

4reactions
manklucommented, Oct 16, 2018

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google TypeScript Style Guide
This guide is based on the internal Google TypeScript style guide, ... Do not use trailing or leading underscores for private properties or...
Read more >
The starting point for learning TypeScript
TypeScript Documentation ​​ Quick introductions based on your background or preference.
Read more >
StyleGuide - TypeScript Deep Dive - Gitbook
StyleGuide. An unofficial TypeScript Style Guide. People have asked me for my opinions on this. Personally I don't enforce these a lot on...
Read more >
Typescript : underscore convention for members [closed]
This guide is intended as a prescriptive guide on style, unlike the Microsoft Style Guide linked in another answer which is primarily intended ......
Read more >
Style Guide - ts - Angular
This style guide presents preferred conventions and, as importantly, ... Back to top ... TypeScript tooling makes it easy to identify private vs....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found