Introduction

Introduction

TypeScript allows us to add types to JavaScript, making our code more robust and scalable.

JavaScript and Types

JavaScript is a dynamically-typed language which means that variables aren’t set to a specific type. Statically-typed languages, like C++, require you to specify a type for a variable and trying to set a variable to a value that has a different type will throw an error.

JavaScript’s lack of types can become problematic in big applications with complex integrations. A value of the wrong type could be passed to a function which expects a different type, ultimately leading to an error. However, this might not be known until the app is in production and a user encounters this bug.

An example of this is a function expecting an array and then running an array method, such as .map. If anything but an array is passed to this function then an error would be thrown: Uncaught TypeError: myValue.map is not a function. This is because an array method is being called on a value that is not an array.

Learning TypeScript

Learning TypeScript is challenging and initially it might feel like you are being slowed down trying to add types to your code. You will need to persevere and eventually with time you will find it easy to add the correct types as well as experience the benefits TypeScript has to offer.


Additional resources

TypeScript docs

What Is TypeScript? Pros and Cons of TypeScript vs. JavaScript The TypeScript Origins Documentary