Moving a MERN Codebase to TypeScript, Step by Step

Moving a MERN Codebase to TypeScript, Step by Step

A gradual migration path that adds type safety without stopping feature work.

Rewriting a working JavaScript codebase in one go is risky. TypeScript is designed to be adopted file by file, and that is how I do it.

Start permissive

Add tsconfig.json with allowJs enabled so .js and .ts files live side by side. Turn on strict for new files from day one; old files can be tightened as you touch them.

Type the boundaries first

  • API request and response shapes.
  • Mongoose models and the documents they return.
  • Environment variables, validated at startup.

Boundaries are where wrong assumptions cause bugs, so typing them gives the most value per hour.

Mongoose with TypeScript

interface User {
  email: string;
  name: string;
  role: "admin" | "member";
}

const userSchema = new Schema<User>({
  email: { type: String, required: true, unique: true },
  name: String,
  role: { type: String, enum: ["admin", "member"], default: "member" },
});

export const UserModel = model<User>("User", userSchema);

Share types between client and server

Put API types in a shared folder or package used by both React and Express. When an endpoint changes, the frontend fails to compile instead of failing in production.

Validate at runtime too

TypeScript types disappear at runtime. Use a schema library to validate incoming data and infer TypeScript types from the same schema, so validation and types never drift apart.

More on this topic

Photography
3D Models
Development
Illustrations
Fashion
Digital Art
Packaging
Motion
Illustrations
Video Production
Photography
3D Models
Development
Illustrations
Fashion