Type Inference
A free lesson from Swipe Coding · TypeScript, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
You don't have to annotate every single variable. If you give a variable an initial value, TypeScript looks at that value and infers its type automatically — you get the same safety without typing it out yourself.
let city = "Lagos"; // inferred as string, no annotation needed city = 42; // Error: Type 'number' is not assignable to type 'string'.
This is why so much everyday TypeScript code looks just like plain JavaScript: as long as a variable is declared with an initial value, an explicit annotation would just be repeating information TypeScript already figured out on its own.
let score = 100; // inferred: number let isDone = false; // inferred: boolean let label = "total"; // inferred: string
Inference stops working once there's no initial value to look at. Declare a variable without assigning it right away, and TypeScript can't guess what it's for — that's exactly when an explicit annotation earns its keep.
let total; // no initial value — inferred as 'any', losing type safety let total2: number; // explicit annotation — TypeScript still enforces it once assigned total2 = "oops"; // Error: Type 'string' is not assignable to type 'number'.
When a variable is declared with an initial value, TypeScript automatically ____ its type — no annotation required.
In which situation do you need an explicit type annotation instead of relying on inference?
let count = 5; requires an explicit : number annotation for TypeScript to treat count as a number.
Keep your streak going in Swipe Coding
Swipe through every level of TypeScript on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →