Explain it simply
TypeScript in plain words
TypeScript is still JavaScript, but you are allowed to add small notes that say what kind of value (what type) each thing should be. For example, you write down that `name` must be text (a string) and `count` must be a number.
The TypeScript compiler checks those notes before your code runs. If you pass text where a number belongs, your editor underlines it in red right away. After checking, the notes are removed and plain JavaScript comes out, which browsers and Node.js already understand. Think of it as a spell-checker for JavaScript, except it checks the shape of your data instead of your spelling.
Problem → Why → Solution
The problem: JavaScript quietly accepts the wrong call
Look at a small JavaScript function that works out how many lessons a learner has left. The first call passes the text `"28"` that came from a form; the second call forgets an argument. JavaScript shows no error at all.
The first call only gives the right answer by luck (the `-` operator happens to convert the text to a number). The second call produces `NaN`, and that `NaN` travels all the way to the screen. You find out about the bug when a user sees 'NaN lessons left'.
// Plain JavaScript: nothing describes what total and done should be
function lessonsLeft(total, done) {
return total - done;
}
console.log(lessonsLeft("28", 6)); // 22 - works only by luck
console.log(lessonsLeft(28)); // NaN - forgot an argument, no warningWhy this hurts
JavaScript keeps no record of what kind of values a function expects, so a bug like this only shows up at run time, and only when that exact line actually runs. In TypeScript, once you write `total: number`, the compiler points at both bad calls while you are still typing.
Core concept: a type is a promise checked at compile time
TypeScript was created by Microsoft and is open source. It does one job: it lets you describe the shape of the values and functions in your JavaScript, then checks that the code actually matches that description. Each annotation is a promise (a contract) such as 'this variable only ever holds a number'.
What matters is when the checking happens. Type checking runs at compile time, meaning before the code runs: while you type in the editor, or when you run the `tsc` command. At run time, while the program is actually executing, the types no longer exist, because the compiler has already removed them.
Write annotations
Put a colon and a type after a name, as in `let count: number = 3;`. Function parameters and return values take annotations the same way.
The compiler checks
If any value breaks a promise, you get the file name, the line number and a message such as 'Argument of type string is not assignable to parameter of type number'.
Types are erased, JavaScript comes out
The output no longer contains any `: number`. A browser or Node.js runs it like any other JavaScript.
These three steps are the foundation of the whole course. Later lessons introduce richer types (unions, interfaces, generics), but every one of them follows the same path: write the promise, let the compiler check it, run the JavaScript.
Two things TypeScript deliberately does not do are worth knowing on day one. It does not change how JavaScript behaves: `"2" + 1` is still `"21"` at run time. And it does not make you annotate everything, because it infers many types from the values you assign.
- JavaScript + type notes
- tsc checks the promises
- Types erased, JS emitted
- Browser or Node.js runs it
Types live only before the code runs. They do not automatically check data that arrives at run time (from an API or a form).
General pattern: annotations
let variableName: Type = value;
function functionName(param: ParamType): ReturnType {
return result;
}Whatever follows a colon (`:`) is a type: one for the variable, one per parameter, and one after the parentheses for the return value. Delete the annotations and what remains is exactly JavaScript.
Your first tiny typed program
The program below has one function with two typed parameters and a return type. It greets a learner and says how many lessons remain. Uncomment the last line and you will see exactly why the compiler refuses it.
function welcome(name: string, lessonsLeft: number): string {
return `Hi ${name}, ${lessonsLeft} lessons to go!`;
}
const learner: string = "Aye Aye";
const total: number = 28;
const done: number = 1;
console.log(welcome(learner, total - done));
console.log(welcome("Ko Ko", total));
// welcome(total, learner);
// Error: Argument of type 'number' is not assignable to parameter of type 'string'.Code breakdown
- Typed parameters and return type
function welcome(name: string, lessonsLeft: number): string {One line makes three promises: `name` is a string, `lessonsLeft` is a number, and the function returns a string.
- Annotated variables
const total: number = 28;Variables can be annotated too. For a simple value like this TypeScript could infer the type by itself (you will see that in Basic Types); here it is written out so the shape is visible.
- A call that matches the contract
console.log(welcome(learner, total - done));`total - done` produces a number, which matches the second parameter. The compiler accepts it and the program prints `Hi Aye Aye, 27 lessons to go!`.
- A call the compiler rejects
// welcome(total, learner);This call swaps the arguments. Plain JavaScript would print 'Hi 28, Aye Aye lessons to go!' and look as if it worked. TypeScript reports the error before anything runs.
Hi Aye Aye, 27 lessons to go!
Hi Ko Ko, 28 lessons to go!- Write welcome.ts
- Compiler checks every call
- Types erased into welcome.js
- Node.js runs it and prints
Types match
JavaScript is emitted and the greeting is printed
Types clash
Error with file name, line and the expected type
How this course is organized
The course has five parts, and each part builds on the one before it. Go through them in order: the generics and conditional types in Advanced are combinations of what you learn in Basic and Intermediate.
| Part | What you learn |
|---|---|
| Basic | What TypeScript is, setup, a strict tsconfig, basic types like string, number, boolean |
| Intermediate | unknown and never, functions, objects, arrays, tuples, literal types, enums, ES modules |
| Advanced | Unions, narrowing, interfaces, classes, generics, utility and conditional types, runtime validation |
| Projects | Building a small Task Manager app from scratch in three parts |
| Exercises | Two practice sets that revisit Basic through Advanced |
Every lesson follows the same order: a plain explanation, a problem, the core concept, the general syntax pattern, a worked example explained line by line, common mistakes, a mini challenge and three quiz questions. Run every example yourself and predict the output before you run it. If you know JavaScript variables, functions, arrays and objects, you are ready to start.
In real projects
TypeScript in real projects
Most large web projects today use TypeScript. Frameworks such as React, Vue, Angular and Next.js support it as a first-class option, and Node.js backends, command-line tools and desktop apps like VS Code are written in it. Many npm packages ship type definitions, so the moment you import a library, your editor shows the parameters of every function.
In a team, types double as documentation. To call a function someone else wrote, you read its signature instead of opening its source to guess what it expects. When you rename a field, the compiler lists every place that still uses the old name, so refactoring a large codebase stops being scary and becomes routine.
Common mistakes
Common beginner mistakes
Believing types check data at run time
Types disappear after compilation. If an API sends `{ "age": "thirty" }`, your program accepts the string without complaint even though you wrote `age: number`. Data from outside must be checked at run time by your own code; the Advanced part covers this with zod.
Silencing errors with `any`
Red underlines are annoying, but adding `any` to make them go away tells the compiler to close its eyes. Read the message instead: it says which type was expected and which type arrived. Usually what needs fixing is a real bug in the code, not the annotation.
Only reading the examples
You remember type errors once you have caused them yourself. In every example, pass one wrong argument on purpose and read the message you get.
Mini challenge
Mini challenge: a progress message
Write a typed function `progressMessage` that takes the number of finished lessons and the total, and returns a string like `6/28 lessons done (21%)`.
- Annotate both parameters and the return value
- Round the percentage to a whole number with `Math.round`
- When `total` is 0, show `0%` (never divide by zero)
- Before running, predict what error `progressMessage("6", 28)` would give
// TODO: add number types to done and total, and a string return type
function progressMessage(done, total) {
// TODO: work out the percentage (guard against total === 0)
// TODO: return "<done>/<total> lessons done (<percent>%)"
}
// TODO: print the message for 6 of 28 and for 0 of 0Expected: Prints `6/28 lessons done (21%)` and `0/0 lessons done (0%)`. `progressMessage("6", 28)` is rejected with 'Argument of type string is not assignable to parameter of type number'.
Show hint
Wrap `done / total * 100` in `Math.round`. Check `total === 0` first and use 0 in that case.
Show solution
function progressMessage(done: number, total: number): string {
const percent = total === 0 ? 0 : Math.round((done / total) * 100);
return `${done}/${total} lessons done (${percent}%)`;
}
console.log(progressMessage(6, 28));
console.log(progressMessage(0, 0));
// progressMessage("6", 28); // Error: string is not assignable to numberQuick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- TypeScript is JavaScript plus type annotations, checked by the compiler before the code runs
- The annotation shape is `name: Type`, used on variables, parameters and return values
- Types are erased after compilation, so runtime data still needs your own checks
- Work through the course in order: Basic, Intermediate, Advanced, Projects, Exercises
Announcing TypeScript 7.0 — TypeScript