Should You Be Using TypeScript on Your Next Project?

Tom CockramComputer Science, TechnologyLeave a Comment

In my opinion yes, typescript is amazing and provides a lot of benefits over vanilla JavaScript. First, let me tell you what typescript is and then why you should be using it.

What is TypeScript?

TypeScript is a strict superset of JavaScript that transcompiles (source to source compiler) the code into JavaScript. It is developed by Microsoft and was first released all the way back in 2012. The idea was to provide a language that would provide a solution for the shortcomings of JavaScript. Since then it has gone on to become a staple in frontend frameworks such as Google’s Angular.


Defining variable types

One of the biggest differences with typescript is that it’s a strongly typed language meaning that it has stricter typing rules requiring things like semicolons and a consistent data type for your variables. Below is an example of how this enables you to declare the types of your variables.


JavaScript

`age = 45`

TypeScript

`age: number = 45;`

Also fine is

`age = 45;`


The benefit of this is that the data type can be clearly defined and can help you determine what the variable is. Imagine you have a variable that returns a result from a backend API request and isn’t defined until the call.


JavaScript

`Item;`

TypeScript

`item: [];`


Static type checking

TypeScript can also warn you of errors before you have even compiled the project. In the below example I have declared an array called numbers and then tried to log something called hello which doesn’t exist and then TypeScript will tell me that this doesn’t exist.


Image

However, with JavaScript, you won’t find out about this error until runtime and you hit line 3. This can be very expensive for companies and developers as you’ll then have to go and fix it later on. TypeScript means that you can fix it before anyone ever notices that silly typo.


Image

For me, this singular reason is enough to choose TypeScript because this one feature could save you a lot of headaches as the system gets bigger and it becomes increasingly more difficult to locate problems like this. If you had a very large application you could accidentally use the same variable for the same thing and change data types. An example of this could be where someone wanted to use numbers to store a single integer and then someone else uses it to store an array of integers. With typescript you’ll get warned of this mismatch before you deploy the code giving you plenty of time to sort out your errors.

Coming from another strictly typed language?

If you’re more familiar with strongly typed languages then TypeScript will give you a much easier transition into front end development. Some of the most popular languages such as Java and TypeScript are strongly typed and will look a lot more familiar to you than JavaScript. With this much easier transition, you can get writing typescript code quicker than before with all the great features such as IntelliSense.

To end things off

TypeScript is a wonderful addition to the JavaScript familiar and can help catch problems before you run into them with a live project. If like me, you enjoy a strongly typed language then it’s hard to imagine no longer using TypeScript and certainly no way you’ll want to go back to using JavaScript.


About the Author

A picture of Tom Cockram, the author of this blog

Tom Cockram

Software Developer


Tom is an apprentice APL developer. He was born in Haywards Heath and has grown up in Southwater. At college he studied I.T, Media Studies and Sport BTEC where he realised that he was interested in software. After finishing college he started his career as a Mobile Application Tester before moving into an apprenticeship in software development. More about Tom


Ask Tom about Software Solutions

More Blogs



Other Posts