Some of you may have heard of Typescript; it’s a superset of the wonderful language known as JavaScript. It has one important feature that makes it slightly better for developers and that is types. You may have never used types before, but let’s discuss what they are and why you should use Typescript.

Types

In Javascript, anything can be of any type. The best example of this is when you’re creating variables. You can assign different types of values for the same variable. Now, that can be an advantage at times, maybe you want to change the value of a variable throughout the program, but that can have huge repercussions and should be avoided. Why? Let me explain why.

As programmers, we need to be able to reason about code as quickly as possible. But, when we assign different types of values to a variable in the code, we are essentially adding another layer of complexity. Now, we have to consider when this value gets changed from a number to a string or even a more complex object. This can lead to errors later down the line since Javascript is a language that is interpreted and not compiled; this means that the program can just stop running due to an error you missed while programming. This is where types help you accomplish more of your goals.

Types essentially are just extra syntactic sugar to tell you and the program what the value actually is. This is something you see often in compiled languages like Java, C, C++ and so on. Here’s an example of code with types in Java versus Typescript to illustrate.

Java

Typescript

As you can see, both languages use types in order to clarify what each variable stands for.  In Java, it’s represented by the keywords like int; in Typescript, we use the names of types in Javascript like number. This allows the developers to be sure in their codebase what each object and variable represents. The only difference is that in Typescript, the type information is optional. But, it should be used for the sake of clarity in the codebase; this will help all developers involved in the coding. But, there are more reasons to like Typescript.

The Typescript Advantage

If you have ever used a Javascript compiler like BabelJs, Typescript is also a compiler that compiles down to Javascript. These Javascript compilers allow you to write code that targets specific web browsers or older devices without breaking pre-existing functionality.

The next important feature of Typescript was mentioned above, which is types. Types allow you to improve code clarity and readability, but they remain optional so you can type plain Javascript as you’d like. There are also more interesting features only found in Typescript, that allow for you as the developer to have an experience closer to Java or C#. That brings me to my biggest point.

For developers who want to have a “safer” Javascript experience, Typescript is your best bet. For people who are coming from a language like C# or Java, this will be a more comfortable ride while still leveraging the flexibility of Javascript.

Personally, I’ve come from a Javascript background. Typescript allows me to write code that is safer and less error-prone. This benefits me and also those who use my code in any meaningful way.

Now, next time we will be going over how to develop with Typescript in your applications.

Resources:

Typescript

%d bloggers like this: