JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q55565F
  • License MIT

AST-based Python to TypeScript transpiler. Convert Python code to clean, idiomatic TypeScript with full type preservation.

Package Exports

  • python2ts

Readme

python2ts

Transpile Python to TypeScript — Automatically

npm version npm downloads CI TypeScript License


Stop rewriting Python code by hand. python2ts transforms your Python into clean, idiomatic TypeScript — with full type preservation.

Install

npm install -g python2ts

Usage

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

# Transpile to stdout
python2ts script.py

# Pipe from stdin
cat utils.py | python2ts > utils.ts

What It Does

Your 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

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 help

Programmatic 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 pythonlib

Documentation

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)
  • pythonlib — Python standard library for TypeScript
  • GitHub — Source code, issues, contributions welcome

License

MIT © Sebastian Software GmbH