Lesson 1 / 128%

Lesson 1

Introduction to TypeScript

10 min

Understand what TypeScript is, why it exists and what it brings to JavaScript.

Definition

A type describes the nature of a piece of data and what you are allowed to do with it. The text "hello" is of type string, the number 42 is of type number, and true is of type boolean. You can add two numbers, but you can't divide a text by an object: types let us know these rules in advance.

Typing is the set of types in a program and the checks built on top of them. With static typing, like TypeScript's, these checks happen before the program runs, while you are writing the code.

What is TypeScript?

TypeScript is a language created by Microsoft that adds atype system to JavaScript. In practice, it is JavaScript in which you can specify the nature of the data you work with (text, number, object...), which lets you catch many errors before the program even runs.

TypeScript is said to be a superset of JavaScript: any valid JavaScript code is also valid TypeScript. So you don't have to relearn the language, only add a layer of safety on top of it.

To picture it, think of a label stuck on every box in a warehouse. Without labels, you have to open a box to know what's inside, and sometimes you find out too late. With the label, you know in advance what is stored in it, and you are warned as soon as you try to put something else in. TypeScript's types are those labels.

TypeScript files use the .ts extension instead of .js. The rest of the work goes on as usual: same variables, same functions, same loops. The only difference is that you can annotate your code with types.

TypeScript logo

The problem TypeScript solves

JavaScript is very permissive: nothing stops you from multiplying a text by an object or calling a function with the wrong argument. The error only shows up at runtime, sometimes on a user's machine. TypeScript flags these problems right in your editor, as you write.

Let's take a simple example. Here is a function that adds two numbers, written in TypeScript:

add.ts
function add(a: number, b: number): number {
  return a + b;
}

add(2, 3);   // 5
add(2, "3"); // Error reported by the editor
  • a: number and b: number indicate that both parameters must be numbers.
  • The : number after the parentheses specifies that the function returns a number.
  • The second call passes a text instead of a number: TypeScript underlines it in red before you even run the program.

In plain JavaScript, the same call add(2, "3") would raise no warning: it would return the text "23" instead of 5, and the bug would only be discovered much later, possibly far from where it was born.

Screenshot of a code editor showing a TypeScript error.

How it works

Browsers don't read TypeScript. A tool called the compiler (tsc) reads your .ts files, checks the types, then produces regular .js files that any browser or Node.js can run.

  • 1. You write code in a .ts file, with types.
  • 2. The editor checks the types live and underlines errors as you type.
  • 3. The compiler converts the file into JavaScript, removing the types.
  • 4. The browser runs the resulting JavaScript, as if it had been written by hand.

You don't always have to write the types yourself: TypeScript can often guess them from the values. When you write const age = 30;, it understands on its own that age is a number. This ability is called type inference, and it keeps your code from being cluttered with unnecessary annotations.

The benefits

  • Fewer bugs: type errors are caught before the code runs. A typo in a property name, a forgotten argument or a possibly missing value are flagged right away.
  • Better autocompletion: the editor knows the shape of your data and suggests the right properties. Type a dot after an object, and the list of what it contains appears.
  • Self-documenting code: types explain what each function expects and returns. No need to open its code to know how to use it.
  • Safe refactoring: renaming or changing a structure shows you everything that breaks. You can change your code with confidence, even in a large project.
  • Easier teamwork: when several people write in the same project, types act as a contract between them and prevent misunderstandings about the shape of the data.

Good to know

Browsers don't understand TypeScript: the code is always converted to JavaScript before it runs. Types disappear at that point; they exist only for development.

An important consequence: TypeScript protects you while you write, but it checks nothing while the program is running. For example, data received from a server may not have the shape announced by your types. We will see later how to guard against this.

When should you use it?

TypeScript has become a standard in medium and large web projects, as well as in many team projects. For a small script of a few lines, it isn't essential. But the bigger a project grows, the more valuable types become: they turn the fear of breaking something into a simple list of errors to fix.

Prerequisites and curriculum

This course assumes you know the basics of JavaScript (variables, functions, arrays, objects). You will install TypeScript, discover the basic types, interfaces, unions, classes, generics and utility types, then see how to use it in a real project.

  • Installation: set up the compiler and configure a project.
  • Basic types: string, number, boolean, arrays and objects.
  • Interfaces and unions: describe the shape of an object and allow several possible types.
  • Classes and generics: write object-oriented code and functions that are reusable with different types.
  • Utility types: transform existing types instead of rewriting them.
  • Real project: use TypeScript in a real application.

Key takeaways

  • TypeScript is JavaScript with types added.
  • It catches errors before the code runs, right in the editor.
  • The code is compiled to JavaScript: types disappear at that point.
  • It often guesses types on its own thanks to inference.

Exercise

Think back to a JavaScript bug you have run into (an unexpected "undefined", a value of the wrong type...). Write it down: by the end of the course, you will know how TypeScript would have prevented it. Also try writing, on paper, a function that you want to accept only numbers: which type would you write after each parameter?