Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Basic Types · Lesson 3 of 12

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 colon and a type name — that's genuinely all it takes to lock a variable down. 🔒

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;
: number is the annotation — it tells TypeScript that age must always hold a number, and nothing else.

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'.
This is a compile error, caught before the code ever runs — nothing crashes in production because of it.
🤖
This is exactly the bug TypeScript exists to prevent — silent type drift. 🐛

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
JavaScript happily lets age become a string. Stopping exactly this kind of silent, hard-to-trace mistake is TypeScript's whole job.
Fill in the blank

let age: number = 29; uses a colon to attach a type ____ to the variable.

Quick check

When does TypeScript report an error for assigning a string to a variable typed number?

True or false

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 →