Module P-3·23 min read

tsconfig.json, typing Express handlers and middleware, interfaces vs types, generics, ts-node for dev, tsc for prod — and migrating a JavaScript project incrementally.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module P-3 — TypeScript in Node.js

What this module covers: TypeScript catches an entire class of bugs at compile time that JavaScript silently ships to production. This module covers setting up TypeScript for a Node.js API, the tsconfig.json settings that actually matter, typing Express handlers and middleware correctly, the type utilities you will use daily, and how to migrate an existing JavaScript project incrementally without stopping all other work.


Why TypeScript on the Backend

TypeScript's value is not just "autocomplete". It is a documentation system that the compiler enforces. Consider:

javascript

The TypeScript version:

  • Documents what the function accepts — no need to read the implementation
  • Errors at compile time if you pass userId as a string
  • Errors at the call site if you forget items
  • Autocompletes input. to show all available fields

At scale — hundreds of functions, dozens of developers, months of development — this prevents entire categories of bugs: wrong property names, missing required fields, null dereferences, wrong return type assumptions.


Installation and Setup

bash

Install type definitions for your libraries:

bash

tsconfig.json: The Settings That Matter

json

The single most important setting is "strict": true. It enables:

  • strictNullChecksnull and undefined are not assignable to other types
  • noImplicitAny — variables must have explicit types when they can't be inferred
  • strictFunctionTypes — function parameter types are checked contravariantly
  • Several others

Without strict, TypeScript is considerably less useful. Always start with it on.


TypeScript Project Structure

text

Update package.json scripts:

json

tsc --noEmit type-checks without producing output — fast, use in CI.

A note on dev: ts-node --esm works, but tsx has become the de facto standard for running TypeScript directly, largely displacing ts-node in new projects — it's esbuild-based (much faster), and handles ESM/CJS interop without a --esm flag or loader configuration. Many teams now write "dev": "tsx watch src/index.ts" instead. This course keeps ts-node --esm in the sample scripts since it's the reference implementation, but reach for tsx if you want less configuration.


Typing Your Domain Models

Define your core types once and import them everywhere:

typescript

Typing Express Handlers

Express's built-in types are usable but loose. Here is the correct pattern:

typescript

Augmenting the Express Request Type

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.