Basic Variable Inference
What you'll learn: How TypeScript automatically figures out what type a variable should have based on the value you assign to it.
The Core Idea
When you create a variable and immediately give it a value, TypeScript doesn't need you to explicitly tell it the type. It looks at the value and infers (deduces) the type for you automatically.
Think of it like meeting someone who's wearing a chef's hat and apron — you don't need them to say "I'm a chef" for you to figure that out. Similarly, TypeScript doesn't need you to say a variable is a number when you assign it 42.
How It Works
let age = 25; // TypeScript infers: number
let name = "Alice"; // TypeScript infers: string
let isDone = false; // TypeScript infers: boolean
In each case above, you didn't write : number or : string — TypeScript saw the initializer (the value on the right side of =) and determined the type automatically.
This means:
let count = 10;
count = 20; // ✓ Fine, still a number
count = "hello"; // ✗ Error! TypeScript knows count is a number
Why This Matters
Basic variable inference means you get type safety without extra typing. You don't have to annotate every single variable — TypeScript is smart enough to help you based on what you're actually doing. This keeps your code clean while still catching mistakes.
Key Takeaway: TypeScript infers types from variable initializers automatically — when you assign a number, string, or boolean, TypeScript knows the type without you writing it explicitly.