Stop 04 · JavaScript & TypeScript for BackendTopics · 5/12

TypeScript Basics

Part of the Backend Developer Roadmap

In short

TypeScript catches bugs while you type, before your API ever runs. Spelling mistakes, missing fields and wrong types get flagged in the editor instead of in production logs.

Adding static types to your backend code so mismatched data shapes are caught at compile time instead of causing runtime errors in production.

The mascot sticks colour labels on identical salt and sugar jars on a shelf.

Think of it like this

Labelled jars in the kitchen

Salt and sugar look the same in plain jars. One sleepy morning, your chai gets salted. Put labels on the jars and the mistake becomes almost impossible. TypeScript puts labels on your values so the wrong one doesn't go in.

Watch it happen

From .ts to running code

app.ts
Type check
Strip types
app.js
Node runs

1/5You write app.ts with type labels like price: number.

The big ideas

What you need to know

Annotate the edges

Label function parameters and return types. Inside functions, TypeScript usually infers the rest for you.

Turn on strict

"strict": true in tsconfig.json catches null bugs and missing types. Always start with it on.

Avoid any

any switches checking off for that value. Use unknown when you truly don't know, and narrow it before use.

Types vanish at runtime

TypeScript can't check JSON from a request. You still need runtime validation for outside data.

See it in code

Walk through the code

ts
1function addGst(price: number, rate = 0.18): number {2  return price * (1 + rate);3}4 5addGst(100);        // OK6addGst("100");      // Error: string is not assignable to number7 8let orderId = 42;   // inferred as number9orderId = "abc";    // Error: string is not assignable to number10 11const tags: string[] = ["veg", "spicy"];12let discount: number | null = null; // can be a number or null

1/4Parameter and return types are labelled. rate gets its type from the default value.

You didn't write a type for orderId, but TypeScript inferred it from the first value. Most of the time, inference does the work.

Save yourself the pain

Common mistakes

Oops

Sprinkling any everywhere to make red squiggles go away.

Do this instead

Fix the real type, or use unknown and narrow it. Each any is a hole where bugs slip through.

Oops

Trusting that req.body matches your TypeScript type.

Do this instead

Types aren't checked at runtime. Validate incoming data with a library like Zod, then use the typed result.

Your mission

Catch three bugs

~20 min

Quick check

A request sends { "age": "twenty" } to a route typed as { age: number }. What does TypeScript do at runtime?

Go deeper

  1. 1Convert one small Express route handler to TypeScript, typing the request/response.
  2. 2Learn TypeScript's structural typing — types match by shape, not declared name.
  3. 3Practice letting inference work rather than over-annotating every variable.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge