Typescript : underscore convention for members

43,403

Solution 1

Just name your private variables as you want but don't use _. You could create your own standard and stick to it.

Setters and getters are like any other functions so you can follow the method naming convention.

Do not use "_" as a prefix for private properties.
Use whole words in names when possible.

This is a subjective opinion, feel free to use _ if you must.

Edit: $ can be used to prefix variable names. In my everyday use case I use it in prefixing observable (rxJS) variables.

Edit:

In the case where you have getters then you can use _ to name the field to avoid name conflict.

Solution 2

Those who say, must not use the "_", for them, here is some code from TypeScript site:

class Employee {
    private _fullName: string;

    get fullName(): string {
        return this._fullName;
    } 
    this._fullName = ......
}

Same question on Stackoverflow, you should have a look on it, especially the answer.

For the time being, if accepted, we should not use _, then what are other better ways?

Let's take your example of email, if we will not use _ then, we will come something like this:

member: to,      get/set: emailTo
member: from     get/set: emailFrom 

might be you can think some better name, but every time you need to think, which is not very common in the developer world!

Using _ and the same name for a property is easy to check the code otherwise we will be keep mapping which property to which member.

BUT: If you have been forced by your lead at your company then you can use $ for member and property without it; not a rule but easy way:

class Employee {
    private fullName$: string;

    get fullName(): string {
        return this.fullName$;
    } 
    this.fullName$ = ......
}

The Choice Is Yours!!!

Solution 3

Underscore "_" prefix for private fields is out-of-date style. It's better to name your variable in a readable and friendly way.

See Microsoft Typescript coding convention here

  1. Do not use "_" as a prefix for private properties.

Solution 4

Using underscores as a prefix or suffix in variable names at all is generally not regarded as good practice.

See the Google Typescript Style Guide. This guide is intended as a prescriptive guide on style, unlike the Microsoft Style Guide linked in another answer which is primarily intended for contributors to the TypeScript repository.

_ prefix/suffix: Identifiers must not use _ as a prefix or suffix

https://google.github.io/styleguide/tsguide.html

Share:
43,403

Related videos on Youtube

Jeson Dias
Author by

Jeson Dias

Software Developer at Srijan Technologies

Updated on October 07, 2021

Comments

  • Jeson Dias
    Jeson Dias over 2 years

    I have a class Email

    class Email {
      private _from: string;
      private _to: Array<string>;
      private _subject: string;
    }
    

    It'll create an email object something like:

    {
      _from:'',
      _to:'',
      _subject:''
    }
    

    This seems a little weird to me since I cannot directly use this object to send to a function . Instead I'll have to transform the object so that it doesn't have underscores . So how do I use the underscore convention or do I have to transform the object .

    EDIT : If I do drop the '_'

    How do I name the getters and setters if we name the private variables without underscore? A VSCode plugin called Typescript toolbox creates them something like this

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

    Is $ a good convention ?

  • Jeson Dias
    Jeson Dias over 6 years
    so how do i name the getters and setters if we name the private variables without underscore ? A vscode plugin called Typescript toolbox creates them something like this public get $subject(): string { return this.subject; } Is $ a good convention ?
  • Jeson Dias
    Jeson Dias over 6 years
    Thanks a lot John . But do you think the method naming convention prefixing a '$' is a good practice ?
  • Jeson Dias
    Jeson Dias over 6 years
    since we can't use the same name as the members in get and set . What would be a good practice in that case ?
  • codejockie
    codejockie over 6 years
    get and set can have same name. See here: typescriptlang.org/docs/handbook/classes.html#accessors
  • Jeson Dias
    Jeson Dias over 6 years
    i meant the member private name:string cant be the same as public get name() public set name() This will throw a duplicate identifier
  • codejockie
    codejockie over 6 years
    Okay, you could differentiate it with casing, private field could Pascal cased while the get and set could be Camel cased. Whichever works for you.
  • jool
    jool about 6 years
    This is a very good point! Simple getters (method/accessors) such as isEnabled() / get isEnabled() { return this.isEnabled; } forces us to name the private member like f.e. "this.enabled", which is a little bit awkward (sounds more like a proper name for an event). I guess one could reason that in these rather rare cases one could live with such a naming, however I strive for consistency in my code and for that reason I would have to name all private members without "is/has/did/wants/..." prefix, just in case I sometimes want to expose them as public members. This is clearly an issue!
  • aleung
    aleung over 5 years
    I agree with this answer: If you want to use get and set accessors, you have to prefix the private property with underscore. In all other cases don't use it. stackoverflow.com/a/40591268/94148
  • chocolateboy
    chocolateboy over 4 years
    "These guidelines are meant for contributors to the TypeScript project's codebase. This is NOT a prescriptive guideline for the TypeScript community."
  • Scott Schafer
    Scott Schafer over 4 years
    These days, I prefer not to write the getter at all, but to expose the variable as read-only. Then you just need to write the setter like so: ` class TestClass { public readonly x: number; setX(value: number) { (this as Writeable<TestClass>).x = value; } `
  • Federico Bellucci
    Federico Bellucci about 4 years
    Why has this answer been accepted? It's true that you can name private variables as you please, but also using underscore is indeed the recommended way to do it
  • Sufian
    Sufian over 3 years
    This is the only correct answer here. The other answers are referring to the conventions which are meant for TypeScript contributors (not TypeScript community).
  • Napinator
    Napinator about 3 years
    I would not recommend to use the $ sign, as it is very commonly used to indicate an observable/source.
  • Kieran Ryan
    Kieran Ryan almost 3 years
    Why not use m for member just a thought e.g. mFullName.. as @Napinator points out $ is typically used for observables.
  • Diego Boy Rz
    Diego Boy Rz over 2 years
    This shouldn't be the accepted answer. The resource quoted is NOT a convention, it's an internal guideline followed by a specific team for consistency reasons: we-have-chosen-many-of-them-for-team-consistency
  • aProgger
    aProgger over 2 years
    In our company, we had some troubles using $var. Some people dealing with it thought it was some jQuery thing. We are using your 2nd example since then.
  • Sajith Mantharath
    Sajith Mantharath about 2 years
    ES Lint won't allow this eslint.org/docs/rules/no-underscore-dangle
  • flm
    flm almost 2 years
    @chocolateboy So what? It's still good guidelines for better readability.