AI Hints
Home›TypeScript Tutorial›Getting Started
TypeScript · Lesson 2 of 120

Why Types Help

Catch bugs early.

⏱ 2 min read▶ Video at 0:56🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Imagine a shop that adds delivery to a price. In JavaScript, if the price arrives as text, the plus sign glues the two together, instead of adding them. No error, just a wrong total.

Why Types Help
From the video: Why Types Help
  • Price as text
  • Plus glues them
  • A silent bug

A silent JavaScript bug

Here the price is text, by mistake, and we add a delivery cost of five. This file is plain JavaScript, so nothing checks it. Run it. Twenty five? No, two hundred and five. The text and the number were glued together.

bug.jsjavascript
const price = "20";        // oops: text, not a number
const delivery = 5;
console.log("Total:", price + delivery);
Output
Total: 205

TypeScript catches it

In TypeScript, we say the total must be a number. Now the compiler checks the code before it runs. Run it. A type error, pointing at the exact line, before any customer sees a wrong price.

app.tstypescript
const price = "20";
const delivery = 5;
const total: number = price + delivery;
console.log("Total:", total);
Output
app.ts line 3: Type 'string' is not assignable to type 'number'.

What types give you

Mistakes are caught early, while you type. Your editor can suggest names, and show what each function needs. And big codebases become much easier to change safely.

keyvalue
Bugscaught early
Editorsmart suggestions
Changessafe refactors
Teamsclear contracts

Summary

Types catch mistakes like text plus number, before the code runs. Next, how TypeScript actually runs.

Key takeaways
  • Silent bugs
  • Caught early
  • Better editors
Next up: How It Runs — tsc, then node