Python dominates AI, ML, and data science. TypeScript powers modern web applications. Until now, these worlds rarely met.
python2ts changes that — transpile Python to production-ready TypeScript with full type safety. Your Python algorithms, data structures, and business logic can now run anywhere JavaScript runs.
| Package | Description | Version |
|---|---|---|
| python2ts | AST-based Python to TypeScript transpiler | |
| pythonlib | Python standard library for TypeScript |
|
Python (your algorithm) from dataclasses import dataclass
from typing import Optional
@dataclass
class TreeNode:
value: int
left: Optional["TreeNode"] = None
right: Optional["TreeNode"] = None
def max_depth(node: Optional[TreeNode]) -> int:
if node is None:
return 0
return 1 + max(
max_depth(node.left),
max_depth(node.right)
) |
TypeScript (ready to ship) class TreeNode {
constructor(
public value: number,
public left: TreeNode | null = null,
public right: TreeNode | null = null
) {}
}
function maxDepth(node: TreeNode | null): number {
if (node === null) {
return 0
}
return 1 + Math.max(maxDepth(node.left), maxDepth(node.right))
} |
- Bring models to the browser — Run inference directly on the client
- Share code with your frontend — Same algorithms, same behavior, zero translation errors
- Edge deployment — Cloudflare Workers, AWS Lambda@Edge, Vercel Edge Functions
- Python prototyping → TypeScript production — Iterate fast, ship faster
- No more manual rewrites — Automated, deterministic transpilation
- Type safety preserved — Python type hints become TypeScript types
- 2000+ tests — Battle-tested on real code patterns
- Complete standard library — itertools, functools, collections, datetime, re, and more
- Run anywhere — Browsers, Node.js, Deno, Bun, Workers
Tested on every commit across all major JavaScript runtimes:
Node.js (v22, v24) · Bun · Deno · Browsers (Chrome, Firefox, Safari via Playwright)
| Python Feature | TypeScript Output | Status |
|---|---|---|
| Functions & type hints | Typed functions | ✅ |
| Classes & dataclasses | Classes | ✅ |
| List/dict/set comprehensions | filter/map chains |
✅ |
Pattern matching (match) |
switch/if statements |
✅ |
async/await |
Native async/await | ✅ |
| Decorators | Transformed decorators | ✅ |
with statements |
Try/finally blocks | ✅ |
| f-strings | Template literals | ✅ |
| Standard library | pythonlib imports | ✅ |
# Install
npm install -g python2ts
# Transpile a file
python2ts algorithm.py -o algorithm.ts
# Or pipe it
cat script.py | python2ts > script.ts| Resource | Description |
|---|---|
| Homepage | Project overview and features |
| Getting Started | Installation and first steps |
| Syntax Reference | Python → TypeScript transformation rules |
| Runtime Library | Using pythonlib modules |
| API Reference | Complete API documentation |
pnpm install # Install dependencies
pnpm build # Build all packages
pnpm test # Run tests (2000+ tests)
pnpm lint # Lint codeKey design decisions are documented in docs/adr/:
- Node.js >= 22.0.0
- pnpm >= 9.0.0
MIT © Sebastian Software GmbH