python2ts
raw JSON →python2ts is an AST-based Python-to-TypeScript transpiler that converts Python code into clean, idiomatic TypeScript while preserving type annotations and semantics. The current stable version is 1.4.3 (January 2026), with active development and frequent releases (multiple per week). It supports a wide range of Python features including dataclasses, list/dict comprehensions, pattern matching, f-strings, async/await, decorators, context managers, generators, and standard library modules via the accompanying pythonlib runtime. Unlike other transpilers, python2ts produces readable, maintainable TypeScript output with full type preservation and has minimal runtime overhead, making it suitable for migrating large Python codebases to TypeScript. Requires Node >=22.0.0.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/python2ts/index.js not supported. ↓
error TypeError: python2ts.transpile is not a function ↓
error Cannot find module 'pythonlib/builtins' or similar import in transpiled output. ↓
error SyntaxError: Unexpected token 'export' at ... ↓
Warnings
gotcha The TranspileOptions type is not exported directly from 'python2ts'; import from 'python2ts/types' instead. ↓
deprecated Using '--target es5' in CLI options is deprecated; ES2015+ is required. ↓
gotcha Transpiled code depends on the 'pythonlib' package at runtime; it is not a standalone output. ↓
breaking In pythonlib v2.0.0, hashlib functions digest(), hexdigest(), pbkdf2Hmac(), scrypt(), and compareDigest() now return Promises and must be awaited. ↓
gotcha CLI output defaults to stdout; using -o with an existing file will overwrite without confirmation. ↓
breaking Version 1.0 dropped CommonJS support; the package is ESM-only. ↓
Install
npm install python2ts yarn add python2ts pnpm add python2ts Imports
- default wrong
const python2ts = require('python2ts')correctimport python2ts from 'python2ts' - transpile wrong
import transpile from 'python2ts'correctimport { transpile } from 'python2ts' - TranspileOptions wrong
import { TranspileOptions } from 'python2ts'correctimport type { TranspileOptions } from 'python2ts/types' - CLI wrong
import * as python2ts from 'python2ts'; python2ts.cli()correctimport { cli } from 'python2ts'
Quickstart
import { transpile } from 'python2ts';
const pythonCode = `
def greet(name: str) -> str:
return f"Hello, {name}!"
`;
try {
const result = transpile(pythonCode, {
filename: 'example.py',
runtime: 'pythonlib', // default runtime path
});
console.log(result.code);
// Output:
// import { greet } from "pythonlib/builtins"
// function greet(name: string): string {
// return `Hello, ${name}!`;
// }
} catch (error) {
console.error('Transpilation failed:', error.message);
}