Sikau

TypeScript · Day 2 · about 20 min

Basic types and inference

string, number, boolean — and letting TypeScript guess.

You don't have to write every type

TypeScript is clever: when you write let count = 0, it already knows count is a number. This is type inference. Good TypeScript writes types where they help and lets inference do the rest.

The primitive types

let city = "Pokhara";
let altitude = 822;
let open = true;

altitude = altitude + 10;
city = 42;

const greeting = "Namaste";
  • string, number, boolean — the everyday primitives.
  • null and undefined — "nothing here" (TypeScript tracks these carefully in strict mode).
  • Annotate function parameters (TypeScript can't guess those) and anything declared without a value: let total: number;

The rest of this lesson

  • When to write typesexplain
  • const versus let inferenceexplain
  • What type is it?predict
  • Inferred typespredict
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 24 lessons of TypeScript