mts-migrator
v2.1.11
Published
js to ts migration engine — converts commonjs to esm, infers types from ast analysis, extracts jsdoc annotations, and refines types with the typescript compiler
Downloads
1,139
Maintainers
Readme
A js → ts code migrator,
In the world of misunderstanding, To be As accurate as possible
converts javascript files to typescript — handles module system conversion (commonjs → esm), infers types from ast analysis, extracts jsdoc annotations, refines types with the typescript compiler, and optionally runs eslint --fix.
js → ts migrator by Laxenta INC.
I also have devloped A Wallpaper Engine in Rust/Tauri! Colorwall
Install
you can use it directly via pnpm dlx or npx without installing:
pnpm dlx mts-migrator
# or
npx mts-migratorOR install globally (recommended for frequent cli usage):
pnpm add -g mts-migrator
# or
npm i -g mts-migrator----------------------------------- Cli usage, All commands are listed --------------------------------
when you run mts (or pnpm mts / npx mts-migrator) without a target, it will prompt you if you want to run it on all folders in your current directory. if you specify a folder name, it will recursively search for a folder with that name so you don't have to specify the exact path!
> IF YOU WANT TO USE TO MIGRATE YOUR FOLDER, JUST COPY THIS COMMAND:
# run interactively (will prompt for confirmation)
pnpm mts# Or Migrate (to typescript) a specific folder by name (recursively searches for "components named folder")
pnpm mts components# HELP COMMAND!
pnpm mts --help# use --target explicitly
pnpm mts --target utils# preview changes without writing
pnpm mts src --dry-run# skip ts-morph type refinement
pnpm mts src --no-refine# skip `eslint --fix` post-processing
pnpm mts src --no-lint
```bash
# show help
pnpm mts --helpwhat it does
.js file → parse (babel) → convert modules → inject types → generate .ts
→ refine types (ts-morph) → eslint --fix → donemodule conversion
const fs = require('fs')→import fs from 'fs'const { x } = require('y')→import { x } from 'y'require('dotenv').config()→import 'dotenv/config'module.exports = { x, y }→export { x, y }exports.x = y→export { y as x }- strips
.js/.jsxfrom relative imports - deduplicates imports from the same source
type inference
- literals:
"hello"→string,42→number,true→boolean - constructors:
new Map()→Map<any, any>,new Date()→Date - known apis:
Math.floor()→number,process.env.X→string | undefined - arrays:
[1, 2, 3]→number[] - binary ops:
a + b→numberorany,a === b→boolean - jsdoc:
@param {string} name→name: string
type refinement (ts-morph)
after the initial babel pass, ts-morph uses the typescript compiler to replace any annotations with actually-inferred types where possible.
eslint integration
if eslint is installed in your project, mts will run eslint --fix on the migrated files automatically. if not, it skips silently.
programmatic api
import { migrateCode, codeToAST, injectTypes, convertModuleSystem } from 'mts-migrator';
// quick: convert a code string
const { code, errors } = migrateCode('const x = require("fs");');
// granular: use individual steps
const { ast } = codeToAST(jsCode);
convertModuleSystem(ast);
injectTypes(ast);
const tsCode = astToCode(ast);features
- backups: original files are backed up before migration
- dry run: preview what would change without writing
- error recovery: non-fatal parse errors don't block migration
- migration report: json report with per-file status
- config skip: automatically skips config files (webpack, babel, eslint, etc.)
what it doesn't do
- fix logic bugs in your code
- handle flow types (use flow-to-ts for that)
- replace manually-written type annotations
- guarantee zero
anyin output (but it tries hard)
license
made with <3 by laxenta inc
