Skip to the workspace
Pebellslearn by building
TypeScript Types  ›  Lesson

boolean, number and string

boolean 12 minThe three everyday primitive types are boolean, number and string, always written in lower case.

typescript / boolean, number and string

Explore each scene at your own pace.

01 · The rule

The rule

boolean, number and string

The three everyday primitive types are boolean, number and string, always written in lower case.

Describe primitive inputs precisely and reject operations that mix incompatible values.

Almost every task in our tracker is made of three kinds of value: a yes-or-no flag, a count or an amount, and some text. TypeScript has one primitive type for each, and you will write them more than any other type.

The rule

Three everyday primitives: boolean, number, string — always lower case.

let done: boolean = false;
let id: number = 7;
let estimateHours: number = 1.5;
let title: string = "Buy milk";
  • boolean — exactly two values, true and false.
  • number — every JavaScript number: whole numbers, decimals, negatives, and the special values NaN and Infinity. There is no separate int or float type. (Very large whole numbers use a different primitive, bigint, written like 10n.)
  • string — text in single quotes, double quotes or backticks. Template strings like `Task ${id}` are strings too.

Why lower case? Boolean, Number and String with capitals also exist, but they name JavaScript's wrapper objects, created with new String("x"). You almost never want those. The lower-case names are the primitive types, and they are what your values actually are.

Remember from inference: when a variable has an initial value, the annotation is optional. let done = false is already a boolean. Annotations on primitives matter most on function parameters:

function rename(id: number, title: string): boolean {
  // ...
  return true;
}
rename("7", "Buy bread"); // error: "7" is a string, not a number

TypeScript does no conversion. "7" and 7 are different types, even though JavaScript would sometimes blur them together with ==.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.