Primitives: string, number, boolean
TypeScript adds a type-checking layer on top of JavaScript, but the values at runtime are exactly the same values JavaScript has always had. The three you reach for constantly are string, number and boolean — lower-case, because TypeScript reserves the capitalised String, Number and Boolean for the rarely-used wrapper object types. An annotation is the : type after a name; it tells the compiler what is allowed, and disappears completely once the code is compiled to JavaScript.
You should see
Ada 36 trueconst with a string annotation and print it alongside the others.number type for every numeric value — integers and floats both. There is no separate int or float. Arbitrarily large whole numbers use bigint instead, written with an n suffix like 10n.TS2322: assigning the wrong type
let age: number
age = "thirty"app.ts:2:1 - error TS2322: Type 'string' is not assignable to type 'number'.
2 age = "thirty"
~~~The declaration let age: number commits age to holding only numbers for its entire lifetime. Assigning a string violates that contract, and the compiler catches it before the code ever runs — a whole class of bug JavaScript alone cannot see.
Either assign a real number, or, if the annotation was wrong, change it to the type you actually meant.
let age: number
age = 30- Primitive
- An immutable value that is not an object: string, number, boolean, null, undefined, bigint, symbol.
- Type annotation
- The
: Typewritten after a name to declare what values it may hold. Erased entirely at compile time — it produces no JavaScript. - Static typing
- Checking types before the program runs (at compile time) rather than while it runs. TypeScript's whole job.
