Skip to content

Latest commit

 

History

History
191 lines (140 loc) · 6.81 KB

File metadata and controls

191 lines (140 loc) · 6.81 KB

python2ts

Python         TypeScript

Write Python. Ship TypeScript. Run Everywhere.

CI python2ts coverage pythonlib coverage License

Homepage · Documentation · API Reference


The Bridge Between Two Worlds

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.

Packages

Package Description Version
python2ts AST-based Python to TypeScript transpiler npm
pythonlib Python standard library for TypeScript npm

See It In Action

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))
}

Why python2ts?

For AI/ML Teams

  • 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

For Full-Stack Developers

  • Python prototyping → TypeScript production — Iterate fast, ship faster
  • No more manual rewrites — Automated, deterministic transpilation
  • Type safety preserved — Python type hints become TypeScript types

For Everyone

  • 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

Runtime Support

Tested on every commit across all major JavaScript runtimes:

Node.js    Bun    Deno    Browsers

Node.js (v22, v24) · Bun · Deno · Browsers (Chrome, Firefox, Safari via Playwright)

What Gets Transpiled?

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

Quick Start

# Install
npm install -g python2ts

# Transpile a file
python2ts algorithm.py -o algorithm.ts

# Or pipe it
cat script.py | python2ts > script.ts

Documentation

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

Development

pnpm install    # Install dependencies
pnpm build      # Build all packages
pnpm test       # Run tests (2000+ tests)
pnpm lint       # Lint code

Architecture

Key design decisions are documented in docs/adr/:

Requirements

  • Node.js >= 22.0.0
  • pnpm >= 9.0.0

License

MIT © Sebastian Software GmbH