Typescript Coding Convention
1. Use correct data type annotation ( avoid ‘any’ )
Data type annotation is one of the advantages you have when coding in TS, while JS defines data types in the run time. defining data types in TS saves you from weird run time errors that can occur by mistakes.
Do not use ‘any’ keyword when you know what type of data your variable’s gonna hold. It is recommended that you always define the data type whenever you declare a new variable.
Good Example:
name: string = “Hello”
value: number = 50
isCorrect: boolean = false
2. Use ‘let’ instead of ‘var’
Var is your good old friend while let came into the picture in ES6 version.
Let and const were introduced to reduce some drawbacks TS had with var. Var is either a global scope or a local scope declaration. A var type variable becomes a globally scoped variable when it is defined outside a function/block.
In that case the variable is available to be used anywhere inside your script. Var becomes locally scoped when it is defined inside a function. In those cases it is only accessible inside that function.
There are several drawbacks of var. It can be redeclared, can be called without declaring it. TS won’t show any error but you will end up with surprising outputs.
To avoid this, you should use let instead. let is a blocked scope variable declaration. And you cannot redeclare it. But you can declare the same variable name in different scopes, and each of them will be treated as separate different variables.
3. Use ‘const’ for constants
Const came in to the picture together with let. Const is also a blocked scope type. Also we cannot re-declare a const. These are the similarities between let and const.
However the purpose of const comes with the feature that it’s value cannot be updated either (We could update with let). So always use const when you declare a constant.
4. Use tuples for fixed length arrays
You can use the above ‘marks’ array to store different number of items in different places of the same script. TS is not gonna restrict you as long as you provide all the values with the correct defined data type.
However this can lead to nasty logical errors in cases where the array length is a constant. To avoid these you should use array as a tuple, whenever the size should be a fixed size. Tuple is a properly defined array with each of the expecting value’s data type.
5. Use type aliases in repetitive data types
Assume that you have multiple variables/ objects in your script which all follow the same structure of data types.
to avoid this redundant chunks of type declarations and to re-use types, you can use type aliases.
The additional benefit of using a type alias is, your intention of defining the data is now clearly visible.
6. Decide between ‘any’ and ‘unknown’
Any and unknown does the same help if you look from the surface. They help you to easily refactor JS to TS when JS gives you no clue about the data types. In these kind of scenarios or when you don’t really know what type of data to expect, any and unknown comes to help. But there is a difference.
However, you can do anything with ‘any’, when it comes to calling functions of that variable.
But you can’t do this with unknown. unknown is safer.
If you wanna use unknown variable, you have to do it inside a condition block.
You can use whatever value with both any and unknown type variables.
7. Use access modifiers for class members
TS comes with access modifiers for properties of a class, while classes are always public. You can create public, protected or private properties.
• private: only accessible inside the class
• protected: only accessible inside the class and through subclasses
• public: accessible anywhere
Here, you cannot access the salary unless you use the getSalary method.
But you can access the name through a sub class.
8. Avoid unnecessary comments
Commenting is good as long as it is absolutely necessary. Always try to use intuitive names when naming variables and functions. It will reduce the necessity for you to add comments.
Do not comment out source code lines. In debugging it is acceptable but keep in mind not to push commented out source code.
This is some best practice for TS you can research and apply other if necessary for coding Links[1] Vuejs Guide, https://v2.vuejs.org/v2/style-guide/Coding conventions
Last updated
Was this helpful?