useragestringisMembernumberbooleanscoresnumber[]1let user: string = "Mia";2let age: number = 12;3let isMember: boolean = true;4let scores: number[] = [90, 85];5scores.push(70);6age = "twelve";age = 13;✕ error TS2322
types.ts: the code, with a type annotation on each new variableBoxes: one per variable, name on the front, type label in front of itToken: the value travelling from its line into its boxLeft: types.ts, six lines of TypeScript. Right: one box per variable, its type label standing in front. Each value hops into its box only if it matches the label: "Mia" is a string, 12 a number, true a boolean, [90, 85] a number[]. The string "twelve" bounces off the number box with error TS2322; 13 fits. tsc 5.9 with no settings writes var instead of let in the JavaScript file, because its default target is an old version of JavaScript. The labels are removed either way.

Type labels

  1. A variable is a named box for a value. In TypeScript you can label the box with a type, written after a colon: user: string. The label says which kind of value the box may ever hold.

Everyday types

The check

What happens

;; step 1/9 · label
line 1: let user: string = "Mia";

let       make a new variable (a box)
user      its name
: string  its type annotation (the label)
= "Mia"   the value to put in

There is also a type called any. It switches the check off for that box, so any value goes in. It is an escape hatch; avoid it while you learn. You can often leave the label off: let age = 12 still makes age a number, because tsc works it out from the value. The next lesson is about this.