lecture 2 of 120 completed

Type annotations and inference

TypeScript works out most types from the values. You write an annotation where it has nothing to read, and you decide which side is wrong when the two disagree.

What you'll learn

  • How TypeScript gives a variable a type when you wrote none
  • The places where an annotation is needed
  • How to decide whether the annotation or the value is wrong

Look at these two lines. Nobody wrote a type.

let copies = 3;
copies = "three";
copies.ts(2,1): error TS2322: Type 'string' is not assignable to type 'number'.

TypeScript still reports an error, and it names the type number. It read the value 3 on line 1 and decided that copies holds a number. Working out a type from a value is called inference.

Most of the types in a test file come from inference. You write far fewer annotations than you might expect.

What inference reads

TypeScript reads the value on the right of the =.

const title = "Dune";
const copies = 3;
const available = copies > 0;

const label: string = title;
const count: number = copies;
const flag: boolean = available;

The first three lines have no annotation. The last three prove what TypeScript inferred, because each one compiles. string is the type for text, number is for every number, and boolean is for true and false.

In an editor you can hover over a name to see its type. Do that often while you are learning. It shows you what the compiler believes.

Where you write an annotation

An annotation goes after the name, with a colon: let dueDate: string. There are two places where TypeScript has no value to read.

The first is a variable that gets its value later.

let returned;
returned = "yes";
returned = 4;

This compiles with no error. With no value and no annotation, TypeScript accepts anything in returned, so it protects you from nothing. Add the annotation and the check comes back.

let returned: boolean;
returned = "yes";
loan.ts(2,1): error TS2322: Type 'string' is not assignable to type 'boolean'.

The second place is a function parameter. The value arrives later, from a caller.

function isOverdue(daysLate) {
  return daysLate > 0;
}
loan.ts(1,20): error TS7006: Parameter 'daysLate' implicitly has an 'any' type.

any is the type that switches checking off. Lecture 12 is about it. Here TypeScript says it had to fall back to any because you gave it nothing to read. This error is part of a setting called strict, which this course uses and most test projects turn on.

The fix is one annotation. The return type needs none, because TypeScript infers it from the return line.

function isOverdue(daysLate: number) {
  return daysLate > 0;
}

const overdue: boolean = isOverdue(3);

When the annotation and the value disagree

const memberSince: number = "2019";
member.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.

The compiler says the two sides do not match. It does not say which side is wrong, because it cannot know. You decide, and you decide by looking at how the name is used.

Suppose the next line is 2026 - memberSince. The code needs a number, so the annotation is right and the value is wrong. Write 2019, or convert the text with Number("2019").

Suppose the next line is memberSince.startsWith("20"). The code needs text, so the value is right and the annotation is wrong. Change it to string, or remove it and let inference work.

Changing whichever side is quicker makes the red line go away. It can also move the error to the line that uses the name, or hide a real mistake in the data.

How much to annotate

A habit that works well in test code:

  • Annotate every function parameter.
  • Annotate a variable that has no value yet.
  • Let inference handle a variable that gets its value on the same line.

const room: string = "Atlas" is not wrong. A reviewer may still ask you to remove the annotation, because it repeats what the value already says.

Remember this

TypeScript infers a type from the value when there is one. Write an annotation where there is no value to read: a parameter, or a variable filled in later. When an annotation and a value disagree, look at how the name is used before you change either side.

Reference: Everyday Types in the TypeScript Handbook.

now prove it

Reading is the setup. Solve this exercise in the editor. We check your code with the TypeScript compiler. Then we run it on each case and compare what it prints with the expected output.