Why TypeScript, and what a type checker does
The bugs plain JavaScript only reports at runtime, how a static type checker catches them early, and why every type disappears before your code runs.
Why TypeScript, and what a type checker does
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- JavaScript checks types only while the code runs, so a typo or a wrong argument surfaces as a crash in front of a user, often on a code path nobody tested.
- TypeScript adds a static checker that reads your code without running it and reports mismatches between what a value is and how you use it, before anything ships.
- Types are erased at compile time. The JavaScript that actually runs contains no annotations and no runtime type checks, so TypeScript cannot protect you from bad data arriving from outside your program.
- Typing is gradual. Inference covers most local variables, untyped JavaScript can sit next to typed files, and you choose how strict the checker is, so a team can adopt TypeScript one file at a time.
- The biggest everyday payoff is in the editor: autocomplete, inline errors, and safe renames all come from the same checker that runs in your build.
The bug that waits for a user
Here is a small piece of JavaScript that looks fine:
function formatPrice(product) {
return "$" + product.price.toFixed(2);
}
formatPrice({ name: "Lamp", prise: 49 });
Nothing complains when you save the file. Nothing complains when the app starts. The error only appears when this exact line runs: TypeError: Cannot read properties of undefined (reading 'toFixed'). The cause is a misspelled property, prise, but JavaScript has no way to tell you that until the moment it tries to read price and finds nothing there.
That is how JavaScript handles types: it checks them at runtime, one operation at a time, and only on the code paths that actually execute. If the bad call sits behind a rarely used button, the bug ships, and a user finds it for you.
In a 50-line script this is manageable. In a codebase with hundreds of files and several people changing function signatures, it means every change is a guess about who else depends on the shape you just altered.
A checker that reads instead of runs
TypeScript adds one thing on top of JavaScript: a description of what shape each value should have, and a program that checks your code against those descriptions without running it.
type Product = { name: string; price: number };
function formatPrice(product: Product): string {
return "$" + product.price.toFixed(2);
}
formatPrice({ name: "Lamp", prise: 49 });
// Error: Object literal may only specify known properties,
// but 'prise' does not exist in type 'Product'. Did you mean 'price'?
The same mistake is now a red underline in the editor and a failing build in CI. The checker followed every call to formatPrice, not just the ones your tests happened to run.
Think of it like a building inspector reviewing blueprints. Testing is turning on the lights in each room to see if the wiring works. The inspector instead reads the plan and spots that a circuit is wired to the wrong breaker, without anyone flipping a switch. Both are useful. Only one of them covers rooms you forgot to visit.
This is the difference between a static check (analyzing source code) and a runtime check (observing execution). TypeScript is a static checker.
Types disappear before your code runs
The part that surprises people: none of those types exist when the program runs. Compiling the example above produces roughly this:
function formatPrice(product) {
return "$" + product.price.toFixed(2);
}
This is called type erasure. The annotations are removed, and what remains is plain JavaScript that the browser or Node executes like any other script. TypeScript adds no runtime library and no hidden checks.
Erasure has a direct consequence. TypeScript trusts what you tell it about data from the outside world. If you declare that a fetch response is a Product and the server sends { "error": "not found" }, the checker has no way to know. Validating untrusted input is a separate job, covered later in this roadmap.
It also explains why TypeScript is called a superset of JavaScript. Every valid JavaScript program is already valid TypeScript syntax, and removing the types from TypeScript gives you JavaScript back. The JavaScript language itself is taught in the javascript roadmap; here we only add the type layer.
Gradual, not all-or-nothing
TypeScript doesn’t ask you to annotate everything up front. Three things make adoption gradual:
- Inference. In
const total = 3 * 4;the checker already knowstotalis anumber. Most local variables never need an annotation. - Mixed codebases.
.tsand.jsfiles can live in the same project, so you convert one file at a time. - Adjustable strictness. Compiler options decide how much the checker insists on. You can start lenient and tighten the rules as the codebase gets cleaner.
The everyday win shows up before any build runs. The editor uses the same checker to power autocomplete, jump to definition, and renames that update every caller. When you type product. and see exactly name and price, that list comes from the type.
What the video covers
Net Ninja’s opener makes the practical case: the kinds of mistakes the compiler catches that plain JavaScript lets through, the editor feedback you get while typing, and the fact that TypeScript compiles down to ordinary JavaScript. It moves quickly past two ideas this lesson spends more time on: type erasure, and why it means runtime data still needs checking, and the gradual nature of adoption. Watch it for the side-by-side feel of JavaScript versus TypeScript, then use the sections above for the reasoning underneath.
Where this fits
This is the first lesson of the roadmap. Next comes installing TypeScript and running your first compile, where you will see erasure happen on your own machine.
Check yourself
If TypeScript reports no errors, does that guarantee your program never receives a value of the wrong type at runtime?
No. The checker only verifies the code it can see, and every type is erased before the program runs. A JSON response, a form field, or an untyped library can still hand you something that does not match the type you declared, which is why external data needs a runtime check.
What does the browser or Node actually execute when you write a `.ts` file?
Plain JavaScript. The compiler (or another tool) strips the type annotations and outputs ordinary JavaScript, and that output is what runs. The runtime never sees a TypeScript type.
Why can a team start using TypeScript in an existing JavaScript project without rewriting everything first?
Because TypeScript is a superset of JavaScript and its typing is gradual. Existing `.js` files keep working, new or converted files can be `.ts`, inference fills in many types automatically, and strictness can be raised over time.
What is the difference between a runtime type check and a static type check?
A runtime check happens while the program executes and only covers the paths that actually run, failing with an exception. A static check analyzes the source code without running it and covers every path the checker can follow, failing with a compile-time error before the code ships.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.