What Is Type Inference?
You'll learn how TypeScript can figure out types automatically, so you don't have to write them everywhere.
The Core Idea
Type inference means TypeScript examines your code and determines what types your variables should have — without you explicitly writing type annotations. It's like having a smart assistant who watches what you're doing and fills in the paperwork for you.
Think of it like this: if you hand someone an apple, they don't need you to say "this is an apple" — they can see it themselves. Similarly, when you write let count = 5, TypeScript sees the 5 and thinks "that's clearly a number!"
How It Works
When you initialize a variable with a value, TypeScript looks at that value and assigns the appropriate type:
let message = "Hello"; // TypeScript infers: string
let age = 25; // TypeScript infers: number
let isActive = true; // TypeScript infers: boolean
You didn't write : string or : number, but TypeScript knows. It inferred the types from the initial values.
This also works with arrays:
let scores = [95, 87, 92]; // TypeScript infers: number[]
let names = ["Alice", "Bob"]; // TypeScript infers: string[]
Why This Matters
Type inference reduces the amount of code you write while maintaining type safety. You get all the benefits of types (autocomplete, error checking, documentation) without the verbosity of annotating everything manually.
Key Takeaway: Type inference lets TypeScript automatically determine types from your code's context, giving you type safety without explicit annotations — you write less but stay protected.