Lesson 1 of 10

Your first type

Goal

Catch the bug before the code runs

01

Explanation

In JavaScript, add("2", 3) returns "23" without a single warning, and you only find out about the bug when a user complains.

TypeScript adds types that you write after a colon, as in function add(a: number, b: number): number, and the compiler checks them before the code runs, so you see the mistake while you type instead of a week later.

The types are removed when the code is compiled, so the output is plain JavaScript that runs no slower at all, because types are only an agreement between you and the compiler.

02

Your task

Add types to add so that calling it with a string is a type error, while it still works with numbers.

Checks

5 checks
  1. No type errors
  2. add(2, 3) returns 5
  3. Calling it with a string is a type error
  4. Its result can be assigned to a number without an error
  5. Its result can't be assigned to a string
03

Hints

0/3
main.ts
OutputTypeScript 5.9

Loading the TypeScript compiler. This only happens once. Your browser keeps it after that.

–/5

Checks start once the environment is ready.

5 checks listed under “Your task”.