Introduction to TypeScript

Hi, in this post we are going to talk a brief introduction of Typescript.

TypeScript is coming as a language that have a lot of power in our web applications, mobile applications, NodeJS projects and IoT too.

TypeScript make our programs safer because TypeScript check common mistakes, so the word “safer” means that we can type our code in a safety way.

“Type safety” -> Using types to prevent programs from doing invalid things.

What invalid things TypeScript can help me?

Invalid thing are for example if we want to sum a number in a list, executing a function with a string parameter when it actually need a list of objects, executing a method on an object when that method doesn’t actually exist on the object, importing modules that doesn’t exist.

For example if we want to sum the 5 number with an empty array then JS will evaluates the operation to string and at the end we will have a string result and this is a invalid operation:

let result = 5 + []; // Evaluates to the string 5
console.log("result ---->", result);

Result:

TypeScript is very useful because will tell us about this invalid things and will threw more exceptions instead of making the best of what we gave it.

let result = 5 + []; // Error: Did you really mean to add a number and an array?

TypeScript give us helpful error messages in our editors as we type if we are typing invalid things in our code and TypeScript will catch that errors and will warn us about them.

Nice, I will be writing more about TypeScript in the next posts.

By Cristina Rojas.