🟦
Websites

Writing a Backend in TypeScript: Runtime, Framework and Example

06.12.2026
← All articles

Most people know TypeScript as a frontend language, but it is now just as common on the server. Its main benefit is types: what a function accepts and returns is written in the code, and many bugs are caught at compile time instead of in production. On the backend that matters a lot, because one wrong field name when talking to a database, payment gateway or external API can break a whole order.

Why TypeScript on the backend

With plain JavaScript you often meet "undefined" errors only on a live server after a customer complains. TypeScript flags them in your editor: access a missing property or pass a number instead of a string and the build fails. Types also act as documentation for the team, and if your frontend is in React or Vue you can share request and response types in a common package so API changes surface immediately on both sides.

Runtime: Node.js, Bun or Deno

On Node.js you usually compile with tsc and use a tool like tsx during development. Recent Node.js versions can also run .ts files directly by stripping type annotations, but this only erases types and does not support some syntax such as enum by default. Bun and Deno understand TypeScript out of the box; they are fast and pleasant, yet Node.js remains the most battle-tested choice for production, so the example below uses it.

Choosing a framework

Example: an appointment API for a Namangan clinic

A private clinic in Namangan has a "book a doctor" form on its website. We need a small API that receives, validates and stores those requests.

mkdir klinika-api && cd klinika-api
npm init -y
npm install fastify zod
npm install -D typescript tsx @types/node
npx tsc --init

Enable strict mode in tsconfig.json, since that is where TypeScript earns its keep, and add ES modules and scripts to package.json.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "rootDir": "src",
    "outDir": "dist",
    "strict": true,
    "skipLibCheck": true
  }
}
{
  "type": "module",
  "scripts": {
    "dev": "tsx watch src/server.ts",
    "build": "tsc",
    "start": "node dist/server.js"
  }
}

Types vanish at runtime, so validate

TypeScript types exist only at compile time. JSON from a browser obeys no type, so anything coming from outside must be checked at runtime. Zod lets you write a schema once and derive the type from it.

// src/server.ts
import Fastify from 'fastify';
import { z } from 'zod';

const app = Fastify({ logger: true });

const QabulSchema = z.object({
  ism: z.string().min(2),
  telefon: z.string().regex(/^\+998\d{9}$/),
  shifokorId: z.number().int().positive(),
  sana: z.coerce.date(),
});
type Qabul = z.infer<typeof QabulSchema>;

const qabullar: Qabul[] = [];

app.post('/api/qabul', async (request, reply) => {
  const natija = QabulSchema.safeParse(request.body);
  if (!natija.success) {
    return reply.code(400).send({ xato: natija.error.issues });
  }
  qabullar.push(natija.data);
  return reply.code(201).send({ ok: true });
});

app.listen({ port: 3000, host: '127.0.0.1' });

z.infer derives the Qabul type from the schema, so validation rules and types never drift apart. Invalid input gets a 400 response with details. In a real project an ORM such as Prisma or Drizzle replaces the array and generates types from your tables.

Deploying

A TypeScript backend needs a long-running Node.js process, so it usually lives on a VPS: build on the server, let PM2 run and restart it, and put Nginx in front for port forwarding and SSL. Run the compiled dist output rather than tsx in production, and keep secrets in environment variables. On a sayt.uz VPS you can install Node.js, PM2 and Nginx yourself and follow exactly this setup.

npm ci
npm run build
pm2 start dist/server.js --name klinika-api
pm2 save

Common beginner mistakes

Overusing any switches type checking off; prefer unknown and narrow it. Casting external data with as quiets the compiler but validates nothing. And skipping tsc --noEmit in CI lets type errors slip through. Avoid those three habits and TypeScript will genuinely cut backend bugs.

Related articles

📄 Publishing a PDF Catalogue on Your Site Without Hurting SEO or Speed 📲 Phone Number Sign-Up: Building SMS Verification Codes Properly ⬜ WordPress White Screen of Death: Causes and a Step-by-Step Fix 💳 Connecting Click and Payme to a WooCommerce store
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓