ProgramGuru

Migrating JavaScript Projects to TypeScript

Migrating JavaScript Projects to TypeScript — why it matters

Migrate a JavaScript project gradually: add TypeScript tooling, enable allowJs/checkJs as appropriate, annotate selected modules, and turn on stricter checks incrementally. Runtime tests remain important because changing file extensions does not validate external data or change business logic.

What you’ll learn

  • Explain migrating javascript projects to typescript and the problem it solves.
  • Identify the relevant compile-time checks and runtime responsibilities.
  • Run the example, predict its output, and try a realistic modification.

Understand the mechanism

The TypeScript version of a small JavaScript function makes both numeric parameters explicit. A real staged migration might first add JSDoc to existing .js files and move a subset to .ts.

Renaming foo.js to foo.ts is not enough if modules, untyped dependencies, or runtime assumptions remain incompatible.

Follow the concept flow

Migrating JavaScript Projects to TypeScript — concept flowExisting JS → checkJs / JSDoc → Incremental TS modulesMigrating JavaScript Projects to TypeScript1Existing JS2checkJs / JSDoc3Incremental TS modulesCompiler configuration changes checks and output, not runtime guarantees.
Follow the responsibilities from input to checked result.

Run the primary TypeScript example

Save as migrating-javascript-projects.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.

TypeScript source

/** @param {number} price @param {number} quantity */
function bill(price: number, quantity: number): number { return price * quantity; }
console.log(bill(12, 3));

Verified output

36

Compile: npx tsc --strict --target es2022 --module commonjs migrating-javascript-projects.ts; run: node migrating-javascript-projects.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.

The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.

Trace the result

The TypeScript version of a small JavaScript function makes both numeric parameters explicit. A real staged migration might first add JSDoc to existing .js files and move a subset to .ts.

Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.

Try the full tool or configuration workflow

Practical project lab: These are real configuration and source-file examples, not a TypeScript object that merely describes settings. Place the files at the named paths relative to one project directory.

tsconfig.json

{"compilerOptions":{"allowJs":true,"checkJs":true,"noEmit":true,"strict":true},"include":["src/**/*.js"]}

src/bill.js

/** @param {number} price @param {number} quantity */
function bill(price, quantity) { return price * quantity; }
console.log(bill(12, 3));

Commands

npx tsc -p .

JSDoc and checkJs make a first migration step without renaming every file.

This local compiler or test-runner mini-project was validated in addition to the primary program.

Common mistake and correction

Renaming foo.js to foo.ts is not enough if modules, untyped dependencies, or runtime assumptions remain incompatible.

Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.

Practice the concept

Create a JS file with JSDoc annotations, enable checkJs, then convert it to .ts and compare diagnostics.

Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.

Check your understanding

What should you remember when configuring TypeScript compilation?
Choose one answer. You can try again.

Answer and explanation

Compiler options control static checks or emitted files; running the result is a separate step.

The TypeScript version of a small JavaScript function makes both numeric parameters explicit. A real staged migration might first add JSDoc to existing .js files and move a subset to .ts.

Official reference

For the exact current options and platform behavior, consult the official documentation. These examples were verified using the tool versions described above.

What to remember

Migrate a JavaScript project gradually: add TypeScript tooling, enable allowJs/checkJs as appropriate, annotate selected modules, and turn on stricter checks incrementally.

Run the primary example, then complete the practice task or environment-specific lab.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Upgrading TypeScript Versions →