AI Coding Assistants

Migrate JavaScript to TypeScript with an AI Coding Assistant

A full rewrite is the slowest way to get types. Let an AI coding assistant convert your JavaScript module by module, while tests and the compiler catch every mistake it makes.

FreeLast tested: 2026-11-06Audience: Developers migrating JavaScript codebases

Why gradual beats big-bang

Every team that has tried to rewrite a whole JavaScript codebase in TypeScript in one go knows how it ends: a giant branch that never merges, merge conflicts every morning, and a migration that quietly dies after three weeks. The gradual route works better because TypeScript was designed for it. A .js file and a .ts file can live side by side in the same project, and you can convert one module at a time while the rest of the app keeps shipping.

This is exactly the kind of work an AI coding assistant is good at. Converting a file is mechanical: rename it, add type annotations, fix the errors the compiler reports, and keep the runtime behavior identical. The assistant does the typing; you do the judging. A typical module takes ten to twenty minutes this way, including review, instead of the hour or two it takes by hand. The trick is to give the assistant a tight loop and a way to prove each step worked, instead of asking it to convert the whole project in one prompt.

Set up the safety net first

Before converting a single file, make the project safe to change. An AI assistant will move fast and occasionally break things, so the safety net has to exist before the first conversion, not after. Three things matter: a TypeScript config that accepts JavaScript, a test suite you can run in seconds, and a clean git state so every conversion is one small, reviewable commit.

{ "compilerOptions": { "target": "ES2022", "module": "ESNext", "allowJs": true, "checkJs": false, "strict": false, "noEmit": true, "moduleResolution": "bundler" }, "include": ["src"] }

Start with allowJs: true and strict: false. This lets TypeScript read your JavaScript without complaining about it. Run npx tsc --noEmit and confirm it passes with zero errors before you begin. If your project has no tests, ask the assistant to write a few smoke tests for the modules you plan to convert first — they are the net that catches behavior changes. Our guide to AI-assisted test automation covers that setup in detail.

The migration loop: one module at a time

Use the same loop for every file. It is deliberately boring — boring is what keeps a migration moving for weeks without surprises.

  1. Rename: change utils.js to utils.ts and run the compiler. The errors you see are the real work list.
  2. Prompt with context: paste the file and its callers into the assistant, and ask it to add types without changing behavior. Name the files it may read; do not let it guess at types from memory.
  3. Fix errors in rounds: run npx tsc --noEmit, paste the errors back, and let the assistant fix them. Two or three rounds usually clears a file.
  4. Run the tests: if the tests pass and the compiler is quiet, commit. If a test fails, the conversion changed behavior — stop and look, do not let the assistant "fix" the test to match.
Convert src/utils/format-date.js to TypeScript. Rules: - Do not change any runtime behavior or return values. - Infer types from how the functions are called in src/components/ and src/services/ (read those files first). - Use explicit types, not `any`, unless you explain why. - After converting, run `npx tsc --noEmit` and fix every error. - Then run `npm test` and report the result.

That prompt works because it gives the assistant three things most prompts skip: the files to read for context, a hard rule against behavior changes, and a verification step it must run itself. For more on writing prompts like this, see our prompt engineering techniques for developers.

The hard parts: any, untyped libraries, dynamic code

Most files convert cleanly. The friction comes from a handful of patterns that JavaScript allows and TypeScript does not. The table below shows the usual suspects and the fix that keeps the migration moving instead of stalling on one file for a day.

PatternWhat goes wrongPractical fix
Implicit anyFunction parameters with no typeLet the assistant infer types from callers; type the parameter explicitly
Untyped libraryNo @types package existsWrite a small .d.ts declaration for the functions you actually use
Dynamic objectsProperties added after creationDefine an interface up front, or use Record<string, unknown> as a bridge
Mixed exportsCommonJS require mixed with ES importsConvert to ES imports first, then convert the file

One rule saves more time than any other: when the assistant reaches for any, make it justify it in a comment. A few honest any types in tricky spots are fine. Fifty of them mean the migration produced the appearance of types without the safety, and the next person to touch the file will pay for it. Aim for fewer than five percent of annotations being any, and revisit them in a later pass.

Turning on strict mode in stages

Converting every file with strict: false is only half the job. Strict mode is where TypeScript actually earns its keep — it catches null errors, unhandled promises, and implicit any types. Turn it on in stages, one flag at a time, instead of flipping the whole switch and drowning in a thousand errors.

Each stage is its own commit and its own test run. If a stage produces more than about fifty errors, split it: enable the flag for one folder at a time with a second tsconfig that extends the first. Slow and merged beats fast and abandoned.

How to know you are done

A migration without a finish line drifts forever. Decide the definition of done before you start, and track it visibly. Ours is simple: every source file is .ts or .tsx, allowJs is off, strict is on, the test suite passes, and any appears in fewer than five percent of annotations. When those five boxes are ticked, remove the JavaScript fallback from the config and delete the old .js files.

At that point the migration stops being a project and becomes a habit: new code is written in TypeScript by default, the compiler runs in CI, and the AI assistant's job shifts from converting old code to writing typed code from the start. That is the real payoff — not the converted files, but the fact that the next feature is typed from its first line. If you are also untangling older code as you go, our guide to refactoring legacy code with an AI assistant pairs well with this workflow.

Limits and notes

An AI assistant converts code faster than a person, but it cannot decide what a type should mean — only you know whether a user ID is a string, a number, or a branded type, and a wrong guess baked into fifty files is worse than no type at all. Review every conversion diff, keep modules small, and never let the assistant change a test to make it pass. Migrations also slow down near the end: the last ten percent of files are usually the oldest, weirdest, and most coupled code in the project. Budget extra time for them, and consider leaving a genuinely dynamic corner in JavaScript with a clear comment rather than forcing types onto code that fights them.

Related reading