Lesson 3 / 26

Primitive Types

Annotating string, number and boolean values.

string, number, boolean

A colon after the name declares its type. TS then enforces it everywhere.

let name: string = "Ada";
let age: number = 36;
let isActive: boolean = true;

Why annotate?

Once age is number, assigning a string to it is a compile-time error, not a runtime surprise months later.

A type error

The compiler stops you right here — before the code ever runs.

let age: number = 36;
age = "thirty-six";
// Error: Type 'string' is not assignable to type 'number'.

Quick check: What happens if you assign a `string` to a variable typed `number`?

  • It works, JS is flexible at runtime
  • TypeScript throws a compile-time error
  • The value silently converts to NaN
Answer

TypeScript throws a compile-time error — The TS compiler checks assignments against declared types and refuses to build mismatched code.