The Major Benefits of Using Typescript

The Major Benefits of Using Typescript

Image for post

In several recent conversations, whether they were job interviews or informal networking coffees, I keep hearing one technology mentioned: Typescript. More often that not, the person who says it has their eyes light up when they mention it.

For those who don?t know, Typescript is a strict, typed superset of Javascript developed by Microsoft. It starts from the same Javascript code you?re probably familiar with, and it compiles to Javascript, but it has a few added features that make life easier for programmers.

After hearing all the hype, I decided to look into Typescript for myself. In this post, I will outline the major benefits of using Typescript in applications.

Static Typing

The main benefit of Typescript is that it offers the ability to add static types to your Javascript code. Javascript is a dynamically typed language, which is one of its best or one of its worst features, depending on who you ask.

Static typing allows you to catch errors earlier in the debugging process. For example, let?s say I had a Javascript function that accepted an argument of a number and added five to it:

function addFive(num) { return num + 5}

That all looks good, but num in the function definition doesn?t actually serve any functionality ? it?s merely a placeholder for readability. I could name the variable stringDefinitelyNotANumber and it would still work as planned if I pass in an integer to the function.

This can lead to some unexpected results. For example, if I call addFive(“hello”), the function will return “hello5”. This is known as type coercion. If it can, Javascript will see that it is trying to add a string and a number together, and it will convert the number to its string equivalent and concatenate them.

The problem with type coercion is that, since the function is still able to return a value, it won?t raise an error even though the function is not working as intended. In this instance it?s fairly easy to see what went wrong, but let?s say I pass in “5” without realizing I have a string instead of an integer and receive “55” instead of 10. Later in the program, I might be confused why what I think is a number is raising an error, and it might take me a while to trace it back to a function that didn?t raise any error.

With Typescript, I could do the following:

function addFive(num: number) { return num + 5}

Then, if I try to call addFive(“5”), I will see the following error:

Image for postTypescript Error

Catching the error instantly allows me to save time and check why my input is not the type I think it is, rather than having to trace my steps back much later in the process without being sure where the error is occurring.

?And It?s Optional

I feel it?s worth emphasizing that the static typing available in Typescript is optional, which is a nice benefit. One of the popular complaints in response to a technology like this is the addition of extra boilerplate code, but with Typescript you only have to use the static typing where you think it will be beneficial.

New Features + Browser Compatibility

Typescript mixes the old with the new in the best way possibility. You can use newer features from ES6, ES7, and beyond, and the compiler will convert them to ES5 (or whatever you specify).

This allows your team to make use of newer features that will be the future of Javascript without having to worry about compatibility. The finished code produced by the compiler will be backwards compatible with ES5 and will work just fine with older browsers.

Enhanced IDE Experience

Due to the static types, several IDEs are able to offer even more predictive assistance than usual (with an instance of a class and its attributes, for example). Atom is one of the IDEs that has a great Typescript package worth checking out.

I hope you found this to be a helpful overview of the benefits of Typescript if you?ve been considering whether or not to use it on any upcoming projects. If you?d like to learn more, I encourage you to check out the resources below.

TypeScript in 5 minutes TypeScript

Similarly, try removing all the arguments to the greeter call. TypeScript will let you know that you have called this?

Benefits of Using TypeScript

I discovered TypeScript some years ago while trying to understand how Angular had changed between major versions. As?

Ionic Framework

Ionic is the app platform for web developers. Build amazing mobile, web, and desktop apps all with one shared code base?

Why use TypeScript, good and bad reasons

This article is part of the collection ?TypeScript Essentials?,
this is Chapter one.


No Responses

Write a response