13

Background

I'm wanting to follow the most commonly-practised naming conventions for TypeScript. I've noticed that the official website shows code examples featuring Pascal-case for types and modules and camel-case for just about everything else.

Example

I'm currently implementing a property that encapsulates a backing value:

class SomeClass {
    get status() {
        return statusBackingField;
    }
    set status(newValue: Status) {
        statusBackingField = newValue;
        //Do other work here
    }

    statusBackingField: Status;
}

Problem

The name of the property is status. In C#, I would normally name the property Status and the backing value status. Since the convention is to use camel-case for properties, this doesn't work. I'm not sure which convention I should use for consistency with other TypeScript code in general.

Question

Other languages, such as C# and Java, seem to have official or de facto standard conventions. Is there any such authoritative or de facto standard convention for naming backing fields in TypeScript?

Notes

For the close-voters: please note that I'm not looking for opinions. I'm looking for objective information as requested in the summarised question above.

Sam
  • 40,644
  • 36
  • 176
  • 219

3 Answers3

3

There is no code convention standard for TypeScript. Since it is a superset of JavaScript, following JavaScript code conventions would probably be the correct approach. In such a case you would use an underscore-prefixed property _status. Idiomatically, this also matches the compiler’s use of an underscore-prefixed _this for compiled arrow functions and underscored-prefixed _super for superclasses.

C Snover
  • 17,908
  • 5
  • 29
  • 39
  • +1 A good principle in variable naming is to avoid solely relying on case to differentiate between variables - which is why many C# programmers use the `_name / Name` convention, rather than `Name / name`. – Fenton Jan 21 '14 at 10:18
2

In C# as well as TypeScript we use private _status. In C# the property will be Status. In TypeScript as you mentioned it is status

basarat
  • 261,912
  • 58
  • 460
  • 511
  • In your second sentence, did you mean "property"? – Sam Jan 21 '14 at 00:24
  • 1
    I'm looking for either an authoritative source of a recommendation, such as Microsoft, or some sort of evidence that this convention is the de-facto standard. – Sam Jan 21 '14 at 00:25
  • @Sam this is the convention set the resharper : http://stackoverflow.com/questions/17755809/resharper-7-1-to-property-with-backing-field-moving-fields-out-of-place The generated backing field is `private type _name` you can try it for yourself. – basarat Jan 21 '14 at 00:33
  • @Sam the coding conventions mentions what you said `status` instead of `_status` but microsoft isn't consistent with it. e.g. the EDMX designer will generate `_Status` : https://github.com/ravendb/ravendb/blob/master/Imports/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/FileSystemEntityModel.Designer.cs?source=c#L366-L370 – basarat Jan 21 '14 at 00:36
  • Those examples look like C# code. I'm looking for evidence of a standard naming convention used in TypeScript code. – Sam Jan 21 '14 at 00:39
  • 1
    @Sam the TypeScript team announced pretty early on that they wanted to see what the community did before recommending this kind of thing. Although the MS C# standard states that you shouldn't use an `_` to prefix private fields, it is becoming increasingly accepted that it is better to do that than rely on prefixing a variable with `this.` when there is a conflict. I think it is likely that the underscore convention will become the accepted standard as Basarat says - but don't expect MS to dictate it for you at this stage. – Fenton Jan 21 '14 at 11:21
  • But what do I do with completly public variables with no special get/set function? Since I can't add an underscore, I would have a mix of members with and without `_`. – maja Jan 31 '15 at 09:12
  • a this right moment I'm using a mix of conventions between Java and C#: the property concept for instance is pretty similar to C#, then I use the property with Capital letters; for interfaces I'm using another C# convention, that is with the Capital I before the name (I was influenced by angular.d.ts); eventually I hate the underscore for fields, then none of them before or among the letters (as a Java convention specifies). – Alexian Oct 14 '15 at 14:48
0

I think it's safe to say at this stage that there is no standard.

If someone can point me to an authoritative standard or evidence of a de-facto standard, then I may consider accepting their answer instead.

Sam
  • 40,644
  • 36
  • 176
  • 219
  • The typescript handbook suggests `_leadingUnderscore`. Read more here: https://www.typescriptlang.org/docs/handbook/classes.html#accessors. – Emric Månsson Sep 12 '19 at 05:48