1. Concept library
  2. JavaScript & TypeScript
  3. Module 1: Types, values and inference

Concept

Type inference

TypeScript works out most types from the values you assign and return, so annotations are for boundaries, not every line. Reading what it inferred is the fastest way to find a mistake.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain how TypeScript infers a type from an initial value or a return statement
  2. Read the inferred type of a variable in the editor and explain it
  3. Decide which places need an annotation and which can rely on inference

In practice

A few lines that show Type inference doing its job.

TypeScript
const port = 8080;              // 8080, a literal type (const)
let host = "localhost";         // string
const tags = ["a", "b"];        // string[]

function double(n: number) {
  return n * 2;                 // return type inferred as number
}
const result = double(port);    // number

The common mistake

Watch for

Annotating everything "to be safe", including values whose type is obvious. It hides the places where an annotation is doing real work, and a wrong annotation beats a correct inference.

What to know first

Your roadmap puts these before Type inference, unless you already know them.

How it stays learned

Once a session shows you understand Type inference, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Type inference.

Previous in the module← Primitive types
Next in the moduleType annotations →