Saltar al contenido principal

TypeScript

xpress-generator has first-class TypeScript support. When you answer "Yes" to the TypeScript prompt, every generated file uses .ts extensions and proper TypeScript syntax.


Enabling TypeScript

During project creation, answer Yes when prompted:

? Use TypeScript? › Yes

That's it. The CLI generates:

  • All source files as .ts
  • tsconfig.json pre-configured
  • ts-node for development
  • ts-jest for testing
  • All necessary @types/* packages installed

TypeScript configuration

The generated tsconfig.json:

{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"skipLibCheck": true
},
"include": ["src/**/*", "tests/**/*"],
"exclude": ["node_modules", "dist"]
}

Key options explained

OptionWhy
"module": "commonjs"Compatible with Node.js require()
"strict": trueEnables all strict checks for safer code
"esModuleInterop": trueAllows import express from 'express' syntax
"outDir": "./dist"Compiled output goes to dist/

npm scripts

TypeScript projects get these scripts in package.json:

{
"scripts": {
"dev": "ts-node src/server.ts",
"start": "node dist/server.js",
"build": "tsc",
"test": "jest --coverage"
}
}
CommandDescription
npm run devRun with ts-node (no compile step, fast for development)
npm run buildCompile TypeScript to dist/
npm startRun the compiled JavaScript from dist/
npm testRun tests with ts-jest

TypeScript file examples

Controller

import { Request, Response, NextFunction } from 'express';
import catchAsync from '../utils/catchAsync';
import httpResponse from '../utils/httpResponse';

export const index = catchAsync(
async (req: Request, res: Response): Promise<void> => {
httpResponse.success(res, { message: 'API running' });
}
);

Custom request type

The generated auth.ts middleware extends Express's Request interface so req.user is typed everywhere:

declare global {
namespace Express {
interface Request {
user?: { id: string; role: string };
}
}
}

Service layer

import Model from '../models/myAppModel';

export async function getAll() {
return Model.find();
}

export async function getById(id: string) {
return Model.findById(id);
}

Generated @types packages

For TypeScript projects, the following dev dependencies are installed automatically:

@types/node
@types/express
@types/jsonwebtoken
@types/bcryptjs
@types/cors
@types/morgan
@types/cookie-parser
@types/supertest

Building for production

npm run build

Output is placed in dist/. To run in production:

npm start
# equivalent to: node dist/server.js
tip

Always run npm run build before deploying. The compiled JavaScript in dist/ is what runs in production — ts-node is only for development.


Generating code in a TypeScript project

The generate:model and generate:middleware commands read .xpress.json to detect the project language and automatically generate .ts files:

npx xpress-generator generate:model Product
# generates src/models/productModel.ts (not .js)