TypeScript with Express or Fastify
TypeScript with Express or Fastify — purpose and prerequisites
Express and Fastify are Node web frameworks. Both can attach handlers, parse JSON, add plugins or middleware, and define routes; their typing, schema, and plugin APIs differ. Framework types are convenient but do not replace validation. The primary example models a typed route contract without assuming either package is installed.
What you’ll learn
- Explain the purpose of typescript with express or fastify.
- Identify the typed contract, runtime responsibilities and common pitfalls.
- Run the example and extend its behavior with a practical exercise.
How the idea works
Even when a route parameter is described as a string, it must be converted and validated before use as a database identifier. Companion files show the real framework adapters and their install commands.
Express or Fastify parameter generics describe shapes to TypeScript; incoming URL strings still need runtime validation.
Visualize the flow
Run the checked TypeScript example
This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.
TypeScript source
type Param = { id: string };
function parseId(params: Param): number | null {
const id = Number(params.id);
return Number.isSafeInteger(id) && id > 0 ? id : null;
}
console.log(parseId({ id: "42" }));
console.log(parseId({ id: "abc" }));Verified output
42
nullCompile: npx tsc --strict --target es2022 --module commonjs express-or-fastify.ts; run: node express-or-fastify.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.
Trace the execution
Even when a route parameter is described as a string, it must be converted and validated before use as a database identifier. Companion files show the real framework adapters and their install commands.
Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.
Build the practical integration
Integration companion: The primary example above is self-contained. The files below demonstrate a real framework, runtime, or project setup and may need additional packages or a browser.
express-app.ts
import express from "express";
const app = express();
app.get("/tasks/:id", (req, res) => {
const id = Number(req.params.id);
if (!Number.isSafeInteger(id) || id <= 0) { res.status(400).json({ error: "Invalid id" }); return; }
res.json({ id, title: "Example task" });
});
app.listen(3000);fastify-app.ts
import Fastify from "fastify";
const app = Fastify();
app.get<{ Params: { id: string } }>("/tasks/:id", async (request, reply) => {
const id = Number(request.params.id);
if (!Number.isSafeInteger(id) || id <= 0) return reply.code(400).send({ error: "Invalid id" });
return { id, title: "Example task" };
});
app.listen({ port: 3001 }).catch(console.error);Setup and execution
npm i express fastify
npm i -D typescript @types/node @types/express
npx tsc --strict --esModuleInterop --target es2022 --module node16 --moduleResolution node16 express-app.ts
npx tsc --strict --esModuleInterop --target es2022 --module node16 --moduleResolution node16 fastify-app.tsInstall the selected framework and run one server at a time; this source uses actual Express and Fastify request/reply APIs. The two packages were not installed or runtime-tested in the validation environment.
Validation: This companion requires the named external packages or browser environment. Its runtime behavior was not executed during the course build.
Common mistake and correction
Express or Fastify parameter generics describe shapes to TypeScript; incoming URL strings still need runtime validation.
Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.
Your turn: extend the example
Create a /hello/:name endpoint in a real framework project and test the missing/invalid cases.
Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.
Check your understanding
Additional official reference
Review the official documentation for the provider, framework or runtime, especially when installing a newer major version.
Key idea to remember
Even when a route parameter is described as a string, it must be converted and validated before use as a database identifier. Companion files show the real framework adapters and their install commands.
Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.
Reaching the recap and completing the quizzes are separate milestones.