AST-based Python to TypeScript transpiler. Convert Python code to clean, idiomatic TypeScript with full type preservation.
npm install python2tsTranspile Python to TypeScript — Automatically





---
Stop rewriting Python code by hand. python2ts transforms your Python into clean, idiomatic
TypeScript — with full type preservation.
``bash`
npm install -g python2ts
`bashTranspile a file
python2ts algorithm.py -o algorithm.ts
What It Does
Your Python
`python
from dataclasses import dataclass
from collections import Counter
@dataclass
class WordStats:
text: str
def word_count(self) -> dict[str, int]:
words = self.text.lower().split()
return dict(Counter(words))
def most_common(self, n: int = 5):
counts = Counter(self.word_count())
return counts.most_common(n)
`
Clean TypeScript
`typescript
import { Counter } from "pythonlib/collections"
class WordStats {
constructor(public text: string) {}
wordCount(): Map {
const words = this.text.toLowerCase().split(/\s+/)
return new Map(new Counter(words))
}
mostCommon(n: number = 5) {
const counts = new Counter(this.wordCount())
return counts.mostCommon(n)
}
}
`
Supported Python Features
| Feature | Example | Output |
| ------------------------ | ----------------------------- | --------------------------------------------- |
| Type hints |
def foo(x: int) -> str: | function foo(x: number): string |
| Dataclasses | @dataclass class Point: | class Point { constructor... } |
| List comprehensions | [x2 for x in items] | items.map(x => x 2) |
| Dict comprehensions | {k: v for k, v in pairs} | new Map(pairs.map(...)) |
| Pattern matching | match x: case 1: ... | switch/if statements |
| f-strings | f"Hello {name}!" | Hello ${name}! |
| Async/await | async def fetch(): | async function fetch() |
| Decorators | @lru_cache def fib(n): | Transformed decorators |
| Context managers | with open(f) as file: | try/finally blocks |
| Generators | yield from items | yield* items |
| Walrus operator | if (n := len(x)) > 0: | let n; if ((n = len(x)) > 0) |
| Multiple inheritance | class C(A, B): | Mixins |
| Standard library | from itertools import chain | import { chain } from "pythonlib/itertools" |CLI Options
`
Usage: python2ts [options] [file]Arguments:
file Python file to transpile (reads from stdin if omitted)
Options:
-o, --output Write output to file instead of stdout
-r, --runtime Custom runtime library path (default: "pythonlib")
--no-runtime Don't add runtime imports
-v, --version Show version number
-h, --help Show help
`Programmatic API
`typescript
import { transpile } from "python2ts"const python =
const typescript = transpile(python)
console.log(typescript)
// function greet(name: string): string {
// return
Hello, ${name}!
// }
`Runtime Library
The transpiled code uses pythonlib for Python
standard library functions. Install it as a dependency in your project:
`bash
npm install pythonlib
``| Resource | Description |
| ------------------------------------------------------------------------------ | ------------------------------ |
| Homepage | Project overview and features |
| Getting Started | Installation and first steps |
| Syntax Reference | Complete transformation rules |
| API Reference | Programmatic API documentation |
Transpiled code runs everywhere JavaScript runs:
- Node.js (v22, v24)
- Bun
- Deno
- Browsers
- Edge (Cloudflare Workers, AWS Lambda, Vercel)
- pythonlib — Python standard library for TypeScript
- GitHub — Source code, issues, contributions
welcome
MIT © Sebastian Software GmbH