Why types catch bugs
You'll learn what TypeScript adds to JavaScript, and why it finds mistakes before your code runs.
Watch & read
Start with the required ones. The others are there when you need them.
See it step by step
The same mistake, in JavaScript and in TypeScript.
Explanation
You write student.nmae instead of student.name. JavaScript says nothing. The app starts, and the
mistake waits. It breaks only when that line runs, maybe on a user's phone, days later.
TypeScript is JavaScript plus labels that say what kind of value each thing is: text, a number, an
object with a name. The kind of value is called its type. A tool called the type checker reads
your whole file before it runs and checks that every value is used the way its type allows.
Think of sending money with bKash. Before anything is sent, the app shows the number and the amount and asks you to check. A mistake caught there costs nothing. A mistake found after sending costs a lot. TypeScript is that check screen for your code.
function withDelivery(price: number) {return price + 60;}withDelivery("200");
price: number is a type annotation: a label that says price must be a number. In plain JavaScript,
withDelivery("200") runs fine and gives "20060", because + joins text. No error, just a wrong total.
In TypeScript, VS Code draws a red underline under "200" while you type:
Argument of type 'string' is not assignable to parameter of type 'number'.
In plain words: you gave text, but this function needs a number.
Two more things to know:
- TypeScript files end in
.ts, or.tsxwhen they contain React screens. - The phone never runs TypeScript. Before the app runs, the types are removed and plain JavaScript is left. Types are only for checking, while you write.
People often think TypeScript checks values while the app is running. It doesn't. It checks your code,
not the data that arrives later from the internet. It also can't catch every mistake: price + 50 instead
of price + 60 is still a number, so it looks fine.
When you read TypeScript, first find the plain JavaScript underneath. Then look for the extra parts after
a colon, like : number. Those are the types. The next topics teach you to read them.
Code tour
Click a line number with a ring to read the note for that line.
Practice
Short drills. Take your time and read the code before you answer.
1function withDelivery(price: number) {2 return price + 60;3}4const total = withDelivery("200");
1const order = { item: "Biryani", price: 250 };2console.log(order.item);3console.log(order.prise);4console.log(order.price * 2);
Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3When does TypeScript tell you about
student.nmae? - 2 / 3What does the phone actually run when your app is written in TypeScript?
- 3 / 3
function withDelivery(price) { return price + 60; }in plain JavaScript. What doeswithDelivery("200")give?
Explain it back
How confident are you with Why types catch bugs?
Done
.ts file, spot the type labels, and say which mistakes TypeScript will catch before the app runs.