bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/TypeScript/TypeScript Core
TypeScript•TypeScript Core

TypeScript Migration Guide

Concept visual

TypeScript Migration Guide

keys map to buckets01kiwi:12pear:43apple:7grape:9

Migrating from JavaScript to TypeScript can significantly improve your codebase's maintainability and developer experience. This guide will walk you through the process step by step.

Preparation Phase

Assess Your Codebase

Before starting the migration:

Identify the size and complexity of your codebase

Document the build process and dependencies

Check for any existing type definitions (.d.ts files)

Identify critical paths that need special attention

Set Up Version Control

Ensure you have a clean git repository or equivalent:

Formula

# Create a new branch for the migration git checkout - b typescript - migration

# Commit your current state git add .

Formula

git commit - m "Pre - TypeScript migration state"

Configuration

Install TypeScript

Formula

# Install TypeScript as a dev dependency npm install -- save - dev typescript @types/node

Create tsconfig.json Create a basic tsconfig.json to start with:

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

Note

Adjust the target based on your minimum supported environments.

Migration Approaches

Gradual Migration

Migrate one file at a time while keeping the rest as JavaScript.

{
"compilerOptions": {
"allowJs": true,
"checkJs": true
}
}

Best for:

Large codebases, minimal disruption

All-at-Once Migration

Rename all.js files to.ts and fix errors. # Rename all JS files to TS

find src -name "*.js" -exec sh -c 'mv "$0" "${0%.js}.ts"' {} \;

Best for:

Small to medium projects, greenfield projects

Important Note

For large projects, we strongly recommend the gradual migration approach to minimize disruption and make the process more manageable.

Step-by-Step Migration

Start with Configuration

Create a basic tsconfig.json with these recommended settings:

{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"rootDir": "./src",
"allowJs": true,
"checkJs": true,
"noEmit": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}

Enable Type Checking for JavaScript

Add

Formula

// @ts - check to the top of your JavaScript files to enable type checking:
// @ts - check
/** @type {string} */
const name = 'John';

Formula

// TypeScript will catch this error name = 42; // Error: Type '42' is not assignable to type 'string'

Note

You can disable type checking for specific lines using

Formula

// @ts - ignore.

Rename Files to .ts

Formula

Start with non - critical files and rename them from.js to.ts

Formula

# Rename a single file mv src/utils/helpers.js src/utils/helpers.ts

# Or rename all files in a directory (use with caution)

find src/utils -name "*.js" -exec sh -c 'mv "$0" "${0%.js}.ts"' {} \;

Add Type Annotations

Gradually add type annotations to your code:

// Before function add(a, b) {
return a + b;
}
// After function add(a: number, b: number): number {
return a + b;
}
// With interface interface User {
id: number;
name: string;
email?: string;
}
function getUser(id: number): User {
return { id, name: 'John Doe' };
}

Update Build and Test Scripts

Modify your package.json to include TypeScript compilation:

{
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"test": "jest"
}
}

Note

Make sure to update your test configuration to work with TypeScript files.

Migration Tools ts-migrate

Formula

Automated tool for migrating JavaScript to TypeScript npx ts - migrate - full .

GitHub Repository

TypeStat

Converts JavaScript to TypeScript with type safety npx typestat

Previous

TypeScript in JavaScript Projects (JSDoc)

Next

TypeScript Error Handling