Understanding Typescript: What it is and why you should care

When starting out in programming, many people find themselves confused about Typescript. What is it, where does it fit, and most importantly, should you learn it before or after React? Let’s explore these questions, but first, let’s break down what Typescript actually is.

What is Typescript?

Typescript is an extension of JavaScript that adds static typing to a language that’s originally dynamic. Some people think of it as a separate language, while others call it a “superset” of JavaScript — a debate we can save for another time. For now, let’s set the labels aside and think about Typescript in practical terms.

Imagine JavaScript as a language we use to give instructions to a computer. It’s clear enough for a machine, designed to interpret it, to execute the commands without any trouble (assuming the instructions are well-defined).

However, there’s one unpredictable element in programming that computers can’t account for: the programmer. We’re human, and as such, we’re prone to fatigue, logic errors, and the chaos that inevitably comes with a bug-filled day.

In dynamic languages like JavaScript, a variable can hold values of different types without immediately causing errors. For example:

let x = 1;
x = "Julia"

console.log(x);
// Console output: "Julia

While the computer has no issue with “x” being both a number and a string, for us programmers, this flexibility can quickly become a headache. If you expect “x” to always be a number and accidentally assign it a string, errors might only show up at runtime — which are much harder to debug.

Where does Typescript come in?

Typescript solves this problem by letting you explicitly define the types of variables, objects, and even function return values. This means it warns you when something doesn’t match the expected type, helping you catch errors before the code is even executed.

Here’s an example:

let x: number = 1;
x = "Julia";

console.log(x);
// Error: Type '"Julia"' is not assignable to type 'number'.

Notice how, by assigning a specific type to the variable “x,” Typescript prevents us from assigning a value of a different type. This significantly reduces errors caused by unexpected values.

Who is Typescript for?

One important thing to note: Typescript is not directly read by the browser. It’s a development tool that needs to be transpiled into JavaScript before it can be executed on the client side. In other words, your Typescript code is converted into plain JavaScript during the build process.

This behavior makes Typescript a powerful tool for developers, without affecting how the browser executes the final code. Think of it as a safety net, helping you identify errors and limiting problematic scenarios while you’re still writing the code.

When should you learn Typescript?

If you’re already comfortable with JavaScript and React, incorporating Typescript into your workflow can be a game-changer. It not only makes your code easier to read and maintain but also increases the reliability of your application.

However, if you’re still learning JavaScript or React, I strongly recommend mastering the fundamentals of these tools before adding Typescript into the mix.

So, the question isn’t if you should learn Typescript, but when.

Comments (1)