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

Types

Part of the Backend Developer Roadmap

In short

Some values can only be one of a few options: an order status, an HTTP method, a size. Type aliases and unions let you say exactly that, and TypeScript holds you to it.

TypeScript's broader type system — unions, intersections, and type aliases — for modeling the different shapes of data flowing through your backend.

The mascot, as a tailor with a measuring tape, sorts kurtas into three ribbon-tied piles.

Think of it like this

The tailor's fixed sizes

The tailor sells kurtas only in small, medium and large. Ask for 'medium-ish' and he just shakes his head. A union type like "S" | "M" | "L" works the same way: only these exact values are allowed, nothing else.

Watch it happen

OrderStatus allows only these

"placed"
0
"packed"
1
"shipped"
2
"delivered"
3
"lost-ish"
4

1/4type OrderStatus = "placed" | "packed" | "shipped" | "delivered". Four exact strings.

The big ideas

What you need to know

type gives a name

type UserId = string or type Point = { x: number; y: number }. Name a shape once, reuse it everywhere.

Unions mean 'one of'

string | number, or exact literals like "GET" | "POST". Perfect for statuses and options.

Intersections combine

A & B means a value with all fields of both. Useful for adding timestamps to any record.

Built-in helpers

Partial<User>, Pick<User, "email">, Omit<User, "password"> make new types from old ones.

See it in code

Walk through the code

ts
1type OrderStatus = "placed" | "packed" | "shipped" | "delivered";2 3type Order = {4  id: string;5  status: OrderStatus;6  total: number;7};8 9type OrderUpdate = Partial<Pick<Order, "status" | "total">>;10type PublicOrder = Omit<Order, "total"> & { totalLabel: string };11 12function update(id: string, changes: OrderUpdate) { /* ... */ }13 14update("o1", { status: "shipped" }); // OK15update("o1", { status: "lost" });    // Error: not an OrderStatus

1/4A union of four exact strings. Nothing else is accepted.

type vs interface: both describe objects. Interfaces can be extended and merged; only type can name unions. Many teams use interface for objects and type for the rest.

Save yourself the pain

Common mistakes

Oops

Typing status as plain string, so "shiped" with a typo slips through.

Do this instead

Use a union of exact strings. The editor then autocompletes and flags typos.

Oops

Writing a second UpdateUser type by hand with every field optional.

Do this instead

Use Partial<User>. When User changes, the update type follows automatically.

Your mission

Type an HTTP request

~15 min

Quick check

type Size = "S" | "M" | "L". Which assignment is an error?

Go deeper

  1. 1Model a request body that can be one of several shapes using a union type.
  2. 2Use a discriminated union to model different event types in a webhook handler.
  3. 3Compare `type` aliases vs `interface` and know when each is preferred.

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