Skip to main content

Basic Types in TypeScript

Let's start with something simple - types are like labels we put on our toys to keep them organized!

What's a Type?

Imagine you have different kinds of toys:

  • Some are numbers (like 5, 42, 100)
  • Some are text (like "hello", "TypeScript")
  • Some are yes/no answers (true or false)

In TypeScript, we tell the computer what kind of toy (data) we're playing with:

// Numbers
let age: number = 5;

// Text (we call this "string")
let name: string = "Tommy";

// Yes/No (we call this "boolean")
let likesIceCream: boolean = true;

Why Do We Need Types?

Types help us:

  1. Avoid mistakes (like trying to add numbers to text)
  2. Get help from our computer when coding
  3. Make our code easier to understand

Try It Yourself!

Can you spot what's wrong here?

let age: number = "five"; // ❌ Oops! Text isn't a number
let age: number = 5; // ✅ That's better!