Introduction
Overview
TypeScript is JavaScript with types. You write the same code you’d write in JavaScript — functions, objects, classes, async/await — and add a few annotations that say what kind of values things hold. A program called the type checker reads those annotations and tells you about mistakes before the code ever runs.
It has become the default way to write serious JavaScript. Most new web frameworks, Node.js backends and npm packages are written in TypeScript, and job listings for front-end and full-stack roles increasingly expect it.
This course takes you from your first annotation to the advanced type features that libraries use, and finishes with a complete, fully typed, tested API.
The problem TypeScript solves
JavaScript is flexible. That’s great for small scripts and painful for big programs:
function orderTotal(prices, shipping) {
let sum = shipping;
for (const price of prices) sum += price;
return sum;
}
orderTotal([7.25, "12.50"], 3); // "10.2512.50" — not 22.75No error, no warning: JavaScript turned the sum into text and carried on. You find out later — maybe from a customer. In a codebase with thousands of functions and several developers, mistakes like these, along with typos in property names and forgotten null checks, are the most common bugs there are.
Here’s the same function in TypeScript:
function orderTotal(prices: number[], shipping: number): number {
let sum = shipping;
for (const price of prices) sum += price;
return sum;
}
orderTotal([7.25, "12.50"], 3);
// ~~~~~~~
// error TS2322: Type 'string' is not assignable to type 'number'.The annotations : number[] and : number say what the function expects. The mistake is underlined in your editor the moment you type it.
How it works: checked, then erased
Two separate things happen to TypeScript code:
- Type checking. The checker reads your whole project and reports anything that doesn’t add up. This happens in your editor as you type, and on the command line with
tsc. - Type erasure. The annotations are removed, leaving plain JavaScript. Browsers and Node.js only ever run JavaScript.
Two consequences follow, and they’re worth remembering from day one:
- Types have zero cost at run time — the JavaScript is as fast as if you’d written it by hand.
- Types do nothing at run time either. They can’t check data that arrives while the program runs, such as a JSON response from an API. You’ll learn how to deal with that in Validating external data.
What you get
- Fewer bugs. Wrong arguments, misspelled properties, impossible
nulls and forgotten cases are caught immediately. - A much better editor. Autocomplete knows every property of every object; you can rename a function across 200 files safely; hovering shows what anything is.
- Code that documents itself.
function send(to: Email, body: string): Promise<void>tells you more than a comment would — and it can’t go out of date. - Confident refactoring. Change a type, and the checker lists every place that needs updating.
The cost is some extra writing and a learning curve. TypeScript is designed to keep both small: it infers most types for you, and you can adopt it gradually, one file at a time.
TypeScript in 2026
- TypeScript 7, released in July 2026, replaced the compiler with a native port written in Go. It checks the same code the same way, but typically 8–12× faster. This course uses TypeScript 7.
- TypeScript 6 (March 2026) modernised the defaults: strict checking is on by default and legacy options are gone. If you read older tutorials, some settings they mention no longer exist.
- Node.js (22.18 and later, including Node 26 LTS) can run
.tsfiles directly by stripping the types, so small projects don’t need a build step at all.
What you need
- Comfort with JavaScript basics: variables, functions, arrays, objects, and ideally
async/await. (Our JavaScript Essentials course covers all of it.) - Node.js 22.18 or newer (Node 26 recommended) and a code editor — the next lesson walks through the setup.
How the course works
Each lesson explains one idea with examples, diagrams of what the type checker is doing, and an exercise. Every exercise solution in this course has been checked with the TypeScript compiler; many use // @ts-expect-error comments to show a line that TypeScript must reject — a technique you’ll use yourself.
Mindset: a type error isn’t the compiler being difficult. It’s a bug report delivered before the bug ships. Read it, and let it teach you.
Let the types find the bug
The starter is plain JavaScript inside a .ts file. It has a bug that JavaScript happily runs: a price arrives as the string "12.50" and gets glued onto the other number instead of added to it. TypeScript already complains that the parameters implicitly have an any type. Annotate the three parameters of orderTotal (a list of numbers, a number and a string) and its return value (a string); TypeScript will then point straight at the bad data. Fix it so the program prints Total: 22.75 USD. Also keep a copy of the bad call inside a function that is never run, with // @ts-expect-error on the line above it, to prove TypeScript catches it.
function orderTotal(prices, shipping, currency) {
let sum = shipping;
for (const price of prices) {
sum += price;
}
return `Total: ${sum.toFixed(2)} ${currency}`;
}
const prices = [7.25, "12.50"]; // one price came from a form, as text
console.log(orderTotal(prices, 3, "USD"));function orderTotal(prices: number[], shipping: number, currency: string): string {
let sum = shipping;
for (const price of prices) {
sum += price;
}
return `Total: ${sum.toFixed(2)} ${currency}`;
}
// TypeScript rejects this call: "12.50" is a string, not a number.
// It's inside a function that is never called, so it never runs.
function badCall() {
// @ts-expect-error
return orderTotal([7.25, "12.50"], 3, "USD");
}
const prices = [7.25, Number("12.50")]; // convert form input before using it
console.log(orderTotal(prices, 3, "USD")); // Total: 22.75 USDSummary
- TypeScript is JavaScript plus a type system: every JavaScript program is valid TypeScript syntax, and you add types where they help.
- Types are checked before the code runs, then erased — the code that actually runs is ordinary JavaScript.
- The checker catches typos, wrong arguments, missing properties and forgotten null checks while you type.
- Types also power the editor: autocomplete, safe renames and inline documentation.
- This course uses TypeScript 7 (the fast native compiler) and Node.js 26, which runs .ts files directly.