Lesson 1 of 25
Why TypeScript
What TypeScript actually is — a type checker that compiles away to plain JavaScript — and the class of bug it catches before your code ever runs.
If you already know JavaScript, you already know most of TypeScript. What you are adding is not a new language to learn from scratch — it is a checker that reads the JavaScript you already write and tells you, before any of it runs, where the shapes of your data don't line up with how you're using it.
The bug TypeScript exists to catch
Here is an ordinary JavaScript bug:
function greet(user) {
return `Hello, ${user.name}!`;
}
greet({ nam: "Priya" }); // typo — "Hello, undefined!"Nothing here throws. user.nam is undefined, template literals happily
stringify undefined, and the function returns a string that is wrong in a
way nobody sees until a person reads the output. This is the shape of an
enormous number of real bugs: not a crash, just quietly wrong data moving
one function further through the program than it should have.
The same code in TypeScript:
function greet(user: { name: string }) {
return `Hello, ${user.name}!`;
}
greet({ nam: "Priya" });
// Argument of type '{ nam: string; }' is not assignable to parameter
// of type '{ name: string; }'.
// Object literal may only specify known properties, and 'nam' does
// not exist in type '{ name: string; }'.The annotation user: { name: string } tells the compiler what shape this
function needs. Nothing about the runtime behaviour changed — it's the same
function, doing the same thing — but the typo is now a compile error instead
of a silent wrong answer.
It compiles away completely
TypeScript is not a new runtime. No browser and no version of Node.js has ever
executed a .ts file directly. A compiler — tsc, or a bundler that wraps
it — reads your annotated source, checks every type, and then erases every
annotation, emitting plain JavaScript:
// input.ts
function double(n: number): number {
return n * 2;
}// output.js — after the compiler runs
function double(n) {
return n * 2;
}That output is indistinguishable from JavaScript you'd have written by hand. This is the single most important fact to hold onto for the rest of this track: types are a compile-time-only conversation between you and the checker. They cost nothing at runtime, and they have no runtime representation — you cannot, for instance, check a variable's declared type from inside a running program, because by the time it's running, that information is gone.
Structural typing: shapes, not names
TypeScript's compatibility rule is structural: a value is assignable to a type if it has the properties that type requires, regardless of what it was "declared as" or what it's named.
type Point = { x: number; y: number };
function distanceFromOrigin(p: Point): number {
return Math.sqrt(p.x ** 2 + p.y ** 2);
}
// Never declared as a "Point" anywhere — it just happens to have
// the right shape, which is all TypeScript asks for.
const location = { x: 3, y: 4, label: "cafe" };
distanceFromOrigin(location); // fine — extra properties are allowed hereThis is different from languages with nominal typing (Java, C#), where a value is only compatible with a type if it was explicitly declared to implement it. TypeScript asks "would this work?", not "was this labelled correctly?" — which tends to match how JavaScript developers already think about objects.
What this buys you day to day
- Autocomplete that's actually correct. Your editor knows
user.nameexists anduser.nmaedoesn't, because it's reading the same types the compiler checks. - Refactors that don't quietly break call sites. Rename a property, and every place that still uses the old name turns red immediately — instead of surfacing as a bug report next week.
- Function signatures as documentation that can't go stale. A parameter
typed
(id: string, options?: { retries?: number })tells you everything a comment would, except a comment can lie and this can't — the compiler enforces it on every call.
None of this replaces tests, code review, or thinking carefully about your program. It catches one specific, extremely common class of mistake — a value's shape not matching what code downstream assumes — earlier and more reliably than any of those can.
Try it yourself
Fix the typo below so the type error disappears, then try changing the parameter type itself and see what new call sites break.
What to remember
- TypeScript is JavaScript plus a compile-time type checker — it compiles away to plain JS with zero runtime cost.
- Nothing executes
.tsfiles directly; a compiler erases every annotation before your code runs anywhere. - Type checking happens at compile time, catching mismatches before the equivalent JavaScript bug would ever surface at runtime.
- Compatibility is structural: TypeScript compares shapes, not declared names.
Check yourself
4 questions · pass 3/4 to unlock Setting Up a TypeScript Project
1.What is TypeScript, precisely?
2.A function expects a
Userobject but is accidentally called with a plain string. When does TypeScript catch this?3.What does TypeScript's "structural" typing mean?
4.After compiling, what does the browser or Node.js actually run?
4 left to answer