What is TypeScript?
TypeScript is JavaScript plus types — it catches whole classes of bugs before your code ever runs.
What you will learn
- Explain what TypeScript adds to JavaScript
- See a bug TypeScript catches that JavaScript misses
- Know that TS compiles down to plain JS
JavaScript, but with a safety net
TypeScript is a language built on top of JavaScript. Every piece of JavaScript you already know is still valid — TypeScript just lets you add types, little labels that say what kind of value something is (a number, a string, a list, and so on).
Those labels let a tool check your code before it runs and warn you when something does not add up — like a spell-checker for your data.
A bug JavaScript happily ignores
Imagine a function that should double a number. In plain JavaScript, nothing stops you from passing it text by mistake:
// Plain JavaScript — no warning, wrong answer
function double(n) {
return n * 2;
}
console.log(double(5)); // we meant to do this
console.log(double('5')); // oops, a string slipped inNote: Output:
10
10
Here both happen to print 10, because JavaScript quietly turns the string into a number. But double('5') + 1 elsewhere would give "51" — and JavaScript never warned you the input was the wrong type.
The same code in TypeScript
Now we add one tiny type label — n: number — to say "this must be a number":
// TypeScript — the mistake is caught instantly
function double(n: number): number {
return n * 2;
}
console.log(double(5)); // fine
console.log(double('5')); // red squiggle here, before runningNote: Output: Error: Argument of type 'string' is not assignable to parameter of type 'number'. TypeScript refuses to compile until you fix it. The bug is caught while you type — not by a confused user later.
Where does the JavaScript come from?
Browsers and Node (Node.js — a program that runs JavaScript on your computer, outside the browser) only understand JavaScript, not TypeScript. So TypeScript is compiled (translated) down to plain JavaScript first. You write .ts files; a tool turns them into .js files that run anywhere.
| JavaScript | TypeScript | |
|---|---|---|
| Types | Optional / none | Yes, checked before running |
| Catches type bugs early | No | Yes |
| Runs in the browser directly | Yes | No — compiles to JS first |
| Editor help (autocomplete) | Basic | Excellent |
Tip: Big projects love TypeScript because the type checker catches mistakes that would otherwise become bugs. Angular is built with TypeScript, and most large React codebases use it too.
Q. What is the main thing TypeScript adds to JavaScript?
✍️ Practice
- Write down two bugs you have hit in JavaScript that a "wrong type" warning could have caught.
- In one sentence, explain why a browser cannot run a
.tsfile directly.
🏠 Homework
- Find one popular tool or framework that uses TypeScript and note one reason its team chose it.