🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
📘 TypeScript

Types for variables

In TypeScript you add types after a colon: let name: string = 'Alex'. The editor and compiler will warn you if you use the wrong type. Here we write valid JS that would be valid TS too (types in comments).

3 min 10 XP Lesson 1 of 21
Types for variables
🌐

Appy Says…

TypeScript adds type safety to JavaScript — it catches bugs before you run the code. That's how teams at Google, Airbnb, and Spotify write millions of lines without entire categories of runtime errors.

📖

What are TypeScript Simple Types?

TypeScript lets you annotate variables, parameters, and return values with a type. The compiler checks that you're using the right types everywhere.

  • let name: string = 'Appy';
  • let age: number = 14;
  • let isAdmin: boolean = false;
  • let score: number; — declared, initially undefined
  • Function: function greet(name: string): string { return 'Hi ' + name; }
  • Type inference: let x = 42; → TS infers x: number automatically
  • Explicit annotation only needed when inference can't determine the type
🎮

Think of it like Minecraft inventory slots

A sword slot only accepts swords — you can't put a carrot there. TypeScript is this slot system for your code. A function expecting a string won't accept a number — TypeScript tells you before the game even starts.

⚙️

How It Works

  • 1. TypeScript is a superset of JavaScript — valid JS is valid TS
  • 2. Annotate: add : Type after variable names and parameters
  • 3. The tsc compiler checks types and outputs plain JavaScript
  • 4. Errors show in your editor immediately (red squiggles) before running
  • 5. No runtime overhead — types are erased at compile time
  • 6. Strict mode ("strict": true in tsconfig) enables the most useful checks
🌍

Real-World Examples

  • function getUser(id: number): User { ... }
  • Passing a string where a number is expected → red squiggle immediately
  • React component: function Button({ label }: { label: string }) { ... }
  • All of Applaa's codebase uses TypeScript — it caught dozens of bugs before they shipped
💡

Key Facts

  • TypeScript was created by Microsoft (Anders Hejlsberg) and open-sourced in 2012
  • Stack Overflow 2024 survey: TypeScript is the 5th most popular language overall
  • Types exist only at compile time — they're completely erased in the output JS
  • TypeScript can infer most types — you don't need to annotate everything
⚠️

Watch Out!

Don't overuse any type — it disables all type checking for that value, defeating the purpose of TypeScript. If you're not sure of the type, use unknown instead — it forces you to check before using the value.

📌

Remember

TypeScript catches type errors at compile time, not runtime. Let inference do the work — only annotate when TS can't figure it out. Avoid any.

What You Learned

  • TypeScript adds type annotations: variable: type, param: type, ): returnType
  • Types are compile-time only — erased in output JS; TS infers most types automatically
  • Unlocks: catching bugs before runtime, editor autocomplete, safer refactoring

Key Facts

  • TypeScript was created by Microsoft (Anders Hejlsberg) and open-sourced in 2012
  • Stack Overflow 2024 survey: TypeScript is the 5th most popular language overall
  • Types exist only at compile time — they're completely erased in the output JS
  • TypeScript can infer most types — you don't need to annotate everything

Real-World Examples

• <code>function getUser(id: number): User { ... }</code> • Passing a string where a number is expected → red squiggle immediately • React component: <code>function Button({ label }: { label: string }) { ... }</code> • All of Applaa's codebase uses TypeScript — it caught dozens of bugs before they shipped

Remember

TypeScript catches type errors at compile time, not runtime. Let inference do the work — only annotate when TS can't figure it out. Avoid any.

Quick Quiz

1 / 2

What does TypeScript add to JavaScript?