Package Exports
- python2ts
Readme
python2ts
Stop rewriting Python code by hand. python2ts transforms your Python into clean, idiomatic TypeScript — with full type preservation.
Install
npm install -g python2tsUsage
# Transpile a file
python2ts algorithm.py -o algorithm.ts
# Transpile to stdout
python2ts script.py
# Pipe from stdin
cat utils.py | python2ts > utils.tsWhat It Does
|
Your Python |
Clean TypeScript import { Counter } from "pythonlib/collections"
class WordStats {
constructor(public text: string) {}
wordCount(): Map<string, number> {
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 | [x*2 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 <file> Write output to file instead of stdout
-r, --runtime <path> Custom runtime library path (default: "pythonlib")
--no-runtime Don't add runtime imports
-v, --version Show version number
-h, --help Show helpProgrammatic API
import { transpile } from "python2ts"
const python = `
def greet(name: str) -> str:
return f"Hello, {name}!"
`
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:
npm install pythonlibDocumentation
| Resource | Description |
|---|---|
| Homepage | Project overview and features |
| Getting Started | Installation and first steps |
| Syntax Reference | Complete transformation rules |
| API Reference | Programmatic API documentation |
Runtime Support
Transpiled code runs everywhere JavaScript runs:
- Node.js (v22, v24)
- Bun
- Deno
- Browsers
- Edge (Cloudflare Workers, AWS Lambda, Vercel)
Related
- pythonlib — Python standard library for TypeScript
- GitHub — Source code, issues, contributions welcome
License
MIT © Sebastian Software GmbH