Why TypeScript

Intermediate

Introduction

JavaScript lets you write total = "ten" where a number was expected, call a function with too few arguments or misspell a property, and you find out only when that line runs, maybe in front of a user. TypeScript is JavaScript with static types. You say what kind of value each variable, parameter and return value holds, and the TypeScript compiler checks the whole program before it runs. Most of the mistakes that crash JavaScript programs become errors on the line where you made them.

TypeScript was created at Microsoft and is now used by most large JavaScript projects: VS Code, Angular, Slack's desktop app and thousands of libraries are written in it. Every program in this course is checked by the real TypeScript compiler, version 5.9, running in your browser in strict mode.

How it works

A type annotation is written after a name with a colon:

let count: number = 0;
const name: string = "Ada";
function double(n: number): number {
  return n * 2;
}

The type checker reads the program and makes sure every value fits where it is used. When one does not, it reports an error with the file, line and column, the error's code and a message:

let total: number = 0;
total = "ten";
// main.ts(2,1): error TS2322: Type 'string' is not assignable to type 'number'.

The code TS2322 is the same in every editor and on every website, so it is worth searching for. In this course, a program with a type error does not run at all; you fix the error first, as a team's build would make you.

You do not have to annotate everything. Type inference works out a type from the value: const free = true is a boolean, and let lessons = 15 is a number. A good habit is to annotate function parameters and return values, which are the promises a function makes, and let inference handle most local variables.

When the checker is happy, TypeScript erases the types and the result is plain JavaScript, which is what runs. That has two consequences. First, types cost nothing at run time. Second, types cannot check data that arrives while the program runs, such as a line typed by the user: prompt() gives text, and its type, string | null, says honestly that it might give nothing at all.

Example

// Types are written after a colon. TypeScript checks them before the program runs.
const course: string = "TypeScript";
let lessons: number = 15;
const free = true; // no annotation: TypeScript infers boolean

function describe(name: string, count: number): string {
  return name + " has " + count + " lessons";
}

console.log(describe(course, lessons));
lessons = lessons + 1;
console.log(typeof course, typeof lessons, typeof free);
console.log(describe(course, lessons));
Open in compiler

Line by line

  1. course is annotated as a string, and lessons as a number.
  2. free has no annotation; TypeScript infers boolean from true.
  3. describe promises to take a string and a number and to return a string.
  4. The call describe(course, lessons) fits, so the program is allowed to run.
  5. typeof at run time sees the JavaScript values: string, number and boolean.
  6. Changing lessons to another number is fine; changing it to text would be a type error.

Expected result

TypeScript has 15 lessons
string number boolean
TypeScript has 16 lessons

In the real world

Teams adopt TypeScript mainly for safety when changing code: rename a property or change a function's parameters, and the checker lists every place that must change too. Editors use the same types for autocompletion and inline documentation. Large companies have reported that a significant share of their production bugs would have been caught by a type checker. TypeScript is also how many npm packages describe their API, through type declaration files that ship with the package.

Common mistakes

  • Annotating everything, even const x: number = 5, which adds noise without adding safety.
  • Silencing errors with any, which turns the checker off for that value instead of fixing the mistake.
  • Expecting types to check input at run time: a value read with prompt() is text until you convert and check it.
  • Ignoring the error code and column: they point exactly at the problem.

Best practices

  • Keep strict mode on in real projects; it is what makes the checker useful.
  • Annotate function parameters and return values; let inference handle the rest.
  • Read the first error first: later errors are often caused by it.

Summary

TypeScript adds static types to JavaScript. Annotations after a colon say what each name holds, inference fills in the obvious ones, and the type checker reports mistakes, with a code such as TS2322, before the program runs. The types are then erased, and the program runs as ordinary JavaScript.

Quick check

1. When does TypeScript report a type error?
2. What is the type of free in const free = true?
3. What happens to type annotations when TypeScript becomes JavaScript?
4. Which code is reported for 'Type string is not assignable to type number'?

Key takeaways

  • TypeScript is JavaScript plus types; the checker reports type errors before anything runs.
  • Annotate parameters and return values; let inference handle most variables.
  • Types are erased: the program that runs is plain JavaScript.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.