Why convert MJS to TS?
Developers convert .mjs to .ts to gain static type checking and editor autocompletion while keeping the same ES-module structure they already wrote.
How to convert MJS to TS
Rename to .ts / .mts EASIEST
Rename the file to .ts (or .mts to force ESM output); the import/export statements work as-is and you add types incrementally.
TypeScript compiler (tsc) OPEN-SOURCE
Set "allowJs": true in tsconfig.json, then run tsc to type-check and emit JavaScript; tighten strict once errors are resolved.
Visual Studio Code FREE
Open the renamed .ts in VS Code; its built-in TypeScript service surfaces missing types and quick-fixes to add annotations.
esbuild OPEN-SOURCE
Run esbuild file.ts --outfile=file.js to strip the types fast for a build, though esbuild does not type-check.
About these formats
An .mjs file is JavaScript source code that Node.js treats as an ECMAScript module, meaning it uses import and export instead of CommonJS require. The extension exists so Node can tell ES…
Open .MJS details →A .ts file is an MPEG Transport Stream - a video container format from digital TV, PVR recordings, AVCHD camcorders, and HLS streaming. VLC media player opens it on any OS. To convert it to…
Open .TS details →Quality & what to watch
- Renaming makes the file valid TS, but it gains no real safety until you add annotations and enable
strictchecks. tscreports type errors that plain JavaScript ignored, so you may need to fix implicitanyand untyped imports.- esbuild transpiles without type-checking, so use
tsc --noEmitalongside it to actually catch type errors.
Frequently asked questions
Is `.mjs` valid TypeScript?
.mjs compiles as .ts even before you add any type annotations.Should I use `.ts` or `.mts`?
.mts when you want the output to stay an ES module regardless of package.json. Use .ts and let tsconfig.json and the package type decide.Do my imports still work?
import/export syntax carries over unchanged into TypeScript. You only add type information on top of it.How do I run the resulting TypeScript?
tsc to JavaScript and run that, or use a runner like tsx or ts-node that transpiles on the fly.