Build a Typed REST API with Express, TypeScript & Zod

Lesson 2 of 8

Setting Up an Express + TypeScript Project

Before writing any routes, get a minimal Express + TypeScript project compiling and running.

Install dependencies

npm init -y
npm install express zod
npm install -D typescript @types/express @types/node tsx
npx tsc --init

express and zod are runtime dependencies, your server actually needs them once it's running. typescript and @types/express/@types/node are dev-only, they exist purely to type-check your code before it ships. tsx runs .ts files directly during development without a separate build step.

Key tsconfig.json options

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "outDir": "dist"
  }
}

"strict": true turns on every strict type-checking flag at once (including the unknown-forcing narrowing rules from the last course), it's the setting that makes TypeScript actually catch bugs instead of just documenting types. Leave it on for anything beyond a throwaway script.

A minimal server

TypeScript

The leading underscore in _req is a convention (not a language feature) for "this parameter is required by the function signature but isn't used in the body," it keeps an unused-parameter lint rule quiet without disabling the check entirely.

TIP

Add "dev": "tsx watch src/index.ts" and "build": "tsc" scripts to package.json, tsx watch re-runs the server on every save during development, tsc produces the compiled dist/ folder you'd actually deploy.

📝 Project Setup Quiz

Passing score: 70%
  1. 1.Why install @types/express as a dev dependency?

  2. 2."strict": true in tsconfig.json enables every strict type-checking flag at once.

  3. 3.app.use(express.____()) parses incoming JSON request bodies into req.body.

  4. 4.What tool runs a .ts file directly during development, without a separate compile step?

  5. 5.express and zod should be installed as regular (non-dev) dependencies, since the running server needs them.