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.
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;
}
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.
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?
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.
The standard naming conventions used in modern software development are as follows: Pascal case. camel case. snake case.
The choice of a variable name should be mnemonic — that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.
The recommended naming and capitalization convention is to use PascalCasing for constants (Microsoft has a tool named StyleCop that documents all the preferred conventions and can check your source for compliance - though it is a little bit too anally retentive for many people's tastes).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With