Skip to content
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

Private variable - leading underscore #861

Open
froodley opened this issue Jul 9, 2018 · 7 comments
Open

Private variable - leading underscore #861

froodley opened this issue Jul 9, 2018 · 7 comments

Comments

@froodley
Copy link

froodley commented Jul 9, 2018

Hi,

The style guide states:

However, this is in direct conflict with how getters and setters work in TypeScript. Because the getter/setter is not a normal function, but instead has the public name of the property, I cannot have a private property propertyX because propertyX will be the name of the getter and setter.

The
public get propertyX(): SomeType { return this._propertyX; }
pattern is the one in the TypeScript docs:
https://www.typescriptlang.org/docs/handbook/classes.html
... and the one used by the tooling by default.

_ is prepended, for instance, if you convert a class with public fields by adding getters and setters in WebStorm. Also, the tslint-consistent-codestyle package's naming-convention default is to enforce _.

https://www.npmjs.com/package/tslint-consistent-codestyle

If another naming convention is preferred over the leading underscore, the style guide should let us know what it is.

@froodley
Copy link
Author

The main VS Code plugin (?) for autogenerating getters and setters
https://marketplace.visualstudio.com/items?itemName=DSKWRK.vscode-generate-getter-setter&_sm_au_=iVV7W3sWs2200Q56

has this behavior:

If a private variable is named with a leading underscore, the getter and setter will be generated in the same way as described in the TS documentation, above.

If it does not have a leading underscore, the getter and setter will be generated with a leading $, and the internal variable will remain unchanged. This does not seem preferable to the internal leading underscore:

public get $var1(): string {
	return this.var1;
}

@froodley
Copy link
Author

froodley commented Aug 8, 2018

Has there been any update here?

@froodley
Copy link
Author

Still looking for feedback on this

@alterfo
Copy link

alterfo commented Sep 7, 2018

I agree

@froodley
Copy link
Author

@pburkindine
Copy link

pburkindine commented Oct 11, 2018

An important concern for us here is that the recommended behavior be lintable.

tslint-consistent-codestyle.naming-convention e.g. allows us to define a pattern for all private fields, but not a rule that would only apply to internal fields mapped by public properties.

So e.g. 'Consider using _ only for internal fields mapped by properties' would not be lintable.

@pburkindine
Copy link

Ref:

angular/angular#26393

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants