Your First Typed Variable
A free lesson from Swipe Coding · TypeScript, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
A type annotation is how you tell TypeScript, up front, exactly what kind of value a variable is allowed to hold. You write it right after the variable name, separated by a colon.
let age: number = 29;
Once a variable has a type, TypeScript holds it to that promise for the rest of its life. Try to assign a value of the wrong type later on, and TypeScript refuses — not while the program is running, but the moment you write the offending line.
let age: number = 29; age = "thirty"; // Error: Type 'string' is not assignable to type 'number'.
Compare that to plain JavaScript, where the exact same reassignment would succeed silently. The bug — age suddenly being a string somewhere deep in the app — might not surface until much later, far from where it actually happened.
// Plain JavaScript — no error here at all let age = 29; age = "thirty"; // perfectly legal, and the mistake goes unnoticed
let age: number = 29; uses a colon to attach a type ____ to the variable.
When does TypeScript report an error for assigning a string to a variable typed number?
In plain JavaScript, reassigning a number variable to hold a string causes an error.
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 →