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.jsonpre-configuredts-nodefor developmentts-jestfor 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
| Option | Why |
|---|---|
"module": "commonjs" | Compatible with Node.js require() |
"strict": true | Enables all strict checks for safer code |
"esModuleInterop": true | Allows 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"
}
}
| Command | Description |
|---|---|
npm run dev | Run with ts-node (no compile step, fast for development) |
npm run build | Compile TypeScript to dist/ |
npm start | Run the compiled JavaScript from dist/ |
npm test | Run 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
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)