The problem with plain JavaScript
JavaScript lets you do almost anything, and it doesn't complain until the code actually runs. In a backend that means a typo can sit quietly in your code until a real user hits it at 2 a.m. Here's a tiny bug from our To-Do app:
// tasks.js (plain JavaScript)
function markDone(task) {
task.don = true; // typo! should be "done"
return task;
}
const task = { id: 1, title: "Buy milk", done: false };
markDone(task);
console.log(task.done); // false π± no error, just a silent bugNo error, no warning. The task just never gets marked done, and you spend an hour finding out why.
The same code in TypeScript
We describe what a task looks like once. Now the editor knows, and it underlines the mistake in red immediately:
// tasks.ts (TypeScript)
type Task = {
id: number;
title: string;
done: boolean;
};
function markDone(task: Task): Task {
task.don = true;
// ^^^ Error: Property 'don' does not exist on type 'Task'. Did you mean 'done'?
return task;
}The bug is found before you even save the file. That's the whole idea: move errors from βa user found itβ to βmy editor found itβ.
6 reasons backends use TypeScript
| Reason | What it means for you |
|---|---|
| Catches typos and wrong data | task.titel, or passing "5" where a number was expected, fail before running. |
| Autocomplete everywhere | Type task. and the editor lists id, title, done. Less looking things up. |
| Safe changes | Rename a field and the editor shows every place you must update. |
| Code explains itself | function createTask(title: string): Task tells you exactly what goes in and out. |
| Database types for free | Prisma generates types from your tables, so queries are checked too. |
| It's what jobs ask for | Most new Node.js backends are written in TypeScript. |
The only 5 things you need to start
You don't need to learn all of TypeScript. This is enough for the whole course:
// 1. Basic types
let title: string = "Buy milk";
let id: number = 1;
let done: boolean = false;
// 2. Arrays
let tags: string[] = ["home", "shopping"];
// 3. Object types: describe the shape once, reuse everywhere
type Task = {
id: number;
title: string;
done: boolean;
dueDate?: Date; // 4. "?" = optional
};
// 5. Type the inputs (and output) of functions
function createTask(title: string): Task {
return { id: Date.now(), title, done: false };
}
createTask("Buy milk"); // β
createTask(42); // β Argument of type 'number' is not assignable to parameter of type 'string'.Most types will be written for you
Good news: in a real backend you write very few types by hand. The tools we use in this course create them for you from things you write anyway:
// You'll rarely write types for database rows yourself.
// Prisma reads your schema and creates them for you:
const task = await prisma.task.findFirst({ where: { id: 1 } });
task.title; // editor knows: string
task.done; // editor knows: boolean
task.titel; // β caught before you run anything
// And Zod creates types from your validation rules:
const createTaskSchema = z.object({ title: z.string().min(1) });
type CreateTaskInput = z.infer<typeof createTaskSchema>; // { title: string }JavaScript vs TypeScript at a glance
| JavaScript | TypeScript | |
|---|---|---|
| When errors show up | When the code runs (maybe in production) | While you type |
| Autocomplete | Guesses | Exact |
| Speed of the running API | Same | Same (types are removed) |
| Extra setup with Bun | None | None: Bun runs .ts files directly |
| Large teams and codebases | Gets risky | Much safer |
Key takeaways
- Plain JavaScript only shows many mistakes when the code runs; TypeScript shows them while you type.
- You get autocomplete, safe renaming and self-explaining code.
- You only need basic types, arrays, object types, optional fields and typed function inputs to start.
- Prisma generates database types and Zod generates input types, so you write few types yourself.
- With Bun, TypeScript needs zero setup: run .ts files directly and type-check with bun check.
Frequently asked questions
Is TypeScript a different language from JavaScript?
Not really. TypeScript is JavaScript plus types. Every JavaScript file is almost valid TypeScript. The types are checked while you write, then removed, so what actually runs is plain JavaScript.
Does TypeScript make my API faster?
No. Types are removed before the code runs, so speed is the same. TypeScript makes you faster and your code safer, not the program.
Is TypeScript hard for beginners?
You only need five things to start: basic types (string, number, boolean), arrays, object types, optional fields with ?, and typing function inputs. Your editor does the rest.
Do companies really use TypeScript for backend?
Yes. Most new Node.js backends at startups and large companies are written in TypeScript, and job posts for Node.js developers very often list it.
Do I need to compile TypeScript?
Not with Bun. Bun runs .ts files directly. To check types without running, use `bun check` (or `tsc --noEmit`).
π§ Quick check: TypeScript
1. When does TypeScript find the mistake task.titel?
2. What runs on the server in the end?
3. In type Task = { title: string; done?: boolean }, what does the ? mean?