Lesson 1
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.
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.

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:
function add(a: number, b: number): number {
return a + b;
}
add(2, 3); // 5
add(2, "3"); // Error reported by the editora: number and b: number indicate that both parameters must be numbers.: number after the parentheses specifies that the function returns a number.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.

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.
.ts file, with types.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.
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.
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.
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.
string, number, boolean, arrays and objects.Exercise