const name: string = "TypeScript"
const year: number = 2025
Lesson 3: Primitive Types: string, number, boolean
TypeScript’s primitive types mirror JavaScript’s: string, number, boolean,
null, and undefined. The key skill is knowing when to annotate explicitly
and when to let TypeScript infer.
Explicit annotations
const name: string = "TypeScript";
const year: number = 2025;
const active: boolean = true;
Inference
Most of the time you don’t need annotations — TypeScript figures it out:
let count = 0; // inferred as number
count = "nope"; // ❌ Error
When to annotate
- Function parameters — always annotate; they can’t be inferred
- Function return types — optional, but good for public APIs
- Empty containers —
const ids: number[] = []
function double(n: number): number {
return n * 2;
}
In the next lesson we’ll look at arrays and tuples.