1. TypeScript in Express
Vanilla Express routes accept req and res as any. That means you can typo a property name, access the wrong field on req.body, or forget a required parameter — and JavaScript will silently let it through until runtime. TypeScript changes that.
The @types/express package ships type definitions for Express. Once installed, every Request and Response object is fully typed.
UrbanCart's Express API serves product data, handles orders, and processes payments. Without types, a developer could accidentally read req.body.productId as a number when it arrives as a string. With TypeScript, the compiler catches that mismatch before deployment.
Extending Express.Request for req.user
The most common real-world requirement is attaching the authenticated user to every request. After your auth middleware runs, you want req.user to be available — typed correctly — in every downstream handler. TypeScript doesn't know about req.user by default, so you use declaration merging to add it.
Express.Request inside declare global, you extend the type that every Express route handler sees — without modifying the library source.
2. tsconfig.json
The tsconfig.json file controls how TypeScript compiles your project. It lives in the root of your project and is read by tsc, ts-node, and frameworks like Next.js automatically.
| Option | What it does | Recommended |
|---|---|---|
| target | JavaScript version to emit. ES2020 for Node 18+, ES5 for legacy browser support. |
ES2020 |
| module | Module format of emitted code. CommonJS for Node, ESNext for bundlers. |
CommonJS (Node) |
| strict | Enables all strict type-checking flags at once. The most important option. | true always |
| outDir | Where compiled .js files are written. |
./dist |
| rootDir | Root directory of TypeScript source files. | ./src |
| paths | Import path aliases. Cleaner imports, no more ../../../ hell. |
Project-specific |
| esModuleInterop | Allows default imports from CommonJS modules. | true |
| skipLibCheck | Skip checking types in node_modules. Dramatically speeds up compilation. |
true |
create-next-app --typescript, Next.js generates a tsconfig.json for you. The key differences from a plain Node project: module is set to ESNext, jsx is set to preserve (Next.js handles JSX), and incremental is true for faster re-builds.
3. Before / After — JS to TS Conversion
Converting an existing Express route from JavaScript to TypeScript is mostly mechanical: rename the file, add type annotations, and let the compiler point you to the gaps. Here's a real-world UrbanCart example.
.js to .ts. Start with // @ts-nocheck at the top to get it compiling, then remove it and fix errors one by one. The compiler becomes your to-do list.
4. TypeScript in Next.js
Next.js has first-class TypeScript support. The framework ships its own types for pages, API routes, middleware, and Server Actions. You get autocomplete for params, type errors for missing props, and typed request/response objects throughout.
Typed Page Components (PageProps)
In Next.js 13+ App Router, every page component receives params and searchParams as props. The PageProps pattern types these precisely.
Typed API Route Handlers
Next.js 13+ API routes use NextRequest and NextResponse instead of the old Express-style req/res objects.
Typed Server Actions
Server Actions are async functions that run on the server — called directly from client components. With TypeScript, you can type their parameters, return values, and error states precisely.
5. Common Patterns — Zod, satisfies, as const
Three TypeScript patterns appear constantly in production Next.js and Node codebases. Understanding them makes reading real-world code much easier.
Zod — Runtime Validation
TypeScript's types disappear at runtime. If an external API sends you malformed JSON, or a form submits unexpected data, TypeScript can't help. Zod validates data shapes at runtime and infers TypeScript types from the schema — so you write the definition once.
The satisfies Operator
Introduced in TypeScript 4.9, satisfies validates that a value matches a type without widening its inferred type. This preserves literal types and enables better autocomplete.
as const — Immutable Literal Types
Zod vs TypeScript
TypeScript checks types at compile time. Zod checks shapes at runtime. Use both: TypeScript for your own code, Zod for external data (API responses, form inputs, environment variables).
satisfies vs : Type
: RouteConfig[] widens the type. satisfies RouteConfig[] validates the shape but keeps the narrowest inferred type. Use satisfies for config objects and constant declarations.
6. Type-safe Environment Variables
By default, process.env.ANYTHING is typed as string | undefined in TypeScript. This means you get no autocomplete on variable names, no error if you misspell DATABASE_URL, and no warning if a required variable is missing. Declaration merging fixes this.
ProcessEnv lives in the NodeJS namespace. By declaring it inside declare global { namespace NodeJS { ... } }, TypeScript merges your additions into the existing interface. Now process.env.DATABASE_URL is typed as string (not string | undefined), and typos like process.env.DATABASE_URLS become a compile error.
Compile-time checks
Runtime validation
Typed env & req.user
Quiz
user to Express's Request type?strict: true in tsconfig.json enable?[slug]?satisfies operator in TypeScript?You completed the TypeScript with Node & Next.js quiz.
You just typed a full-stack app end to end.
Now build a career with the same precision.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →