By Deepak Jayaram on Jul 7, 2016, 4:58:22 AM
Typescript can significantly improve the code quality by helping developers to code in a very efficient way which would reduce a lot of time working around unpredictable errors and typos.
So, how can typescript improve your code quality significantly? Here’s how!
In TypeScript every variable you declare must be assigned a type. There are old types such as string, number and array; but a few interesting ones have been introduced such as any, tuple, union and enums. They are of huge advantage and help to catch errors before it is too late.
Imagine a scenario of calling the
.toUpperCase() on a number. Typically, you will only be notified of this error during run-time; but in typescript your code is checked for accuracy before it runs. You will get a compilation type error stating that a number cannot have
var which is used to declare a variable, Typescript introduces something called as
let is block scoped and allows one to maintain the scope of variables when and where required.
Consider the below example where two 'for' loops are nested. What happens here is, since let is used to declare the variables, the
i in the inside for loop has different scope than the
i on the outer for loop. But if
var was used instead of
i in the inner loop would have gotten overwritten by the value of outer loop
i and the result would be something very different and unforeseen.
Where there are classes, there is Inheritance. Inheritance is where public properties and methods of parent class are allowed access by child classes which inherit the parent.
This is one of the most interesting features of TypeScript. Interfaces are used in TypeScript to define complex types such as an object containing other properties.
Modules help to separate code and maintain them in files which perform a particular task. Doing so would help developers maintain reused code, keep variables out of global scope etc. Below is given an example of modules and how they can be used.
Usage of code blocks is not possible outside the modules if you have not exported them. Use the export keyword to be able to use code block outside a particular module.
There are a ton of new features in TypeScript available to make use of to be able to write less tiring and efficient code. Introduction of new features has made Typescript more flexible and helpful. Few of the features were discussed in brief in this blog. Now it is your turn to go explore TypeScript.
One more important fact to be noted: TypeScript is recommended if you want to work with Angular2!