JSPM

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

AST-based Python to TypeScript transpiler

Package Exports

  • python2ts

Readme

python2ts

npm version License

AST-based Python to TypeScript transpiler — Write Python, ship TypeScript.

Converts Python code to clean, idiomatic TypeScript with full type preservation.

Installation

npm install python2ts

CLI Usage

# Transpile a file
npx python2ts input.py -o output.ts

# Pipe from stdin
cat script.py | npx python2ts > script.ts

# Preview without runtime import
npx python2ts input.py --no-runtime

API Usage

import { transpile } from "python2ts"

const typescript = transpile(`
def fibonacci(n: int) -> list[int]:
    a, b = 0, 1
    result = []
    for _ in range(n):
        result.append(a)
        a, b = b, a + b
    return result
`)

Output:

import { range } from "pythonlib"

function fibonacci(n: number): number[] {
  let [a, b] = [0, 1]
  let result: number[] = []
  for (const _ of range(n)) {
    result.push(a)
    ;[a, b] = [b, a + b]
  }
  return result
}

What Gets Transpiled?

Python TypeScript
int, str, bool, None number, string, boolean, null
list[T], dict[K,V] T[], Record<K,V>
Optional[T] T | null
def fn(): function fn()
lambda x: x + 1 (x) => x + 1
class Child(Parent): class Child extends Parent
@dataclass Auto-generated constructor
for x in items: for (const x of items)
if/elif/else if/else if/else
match/case switch/case
async def / await async function / await
// (floor div) floordiv() (Python semantics)
% (modulo) mod() (Python semantics)
arr[1:-1] (slicing) slice() (full support)
f"Hello {name}" `Hello ${name}`
"""docstring""" /** JSDoc */

Advanced API

import { parse, transform, generate } from "python2ts"

// Step-by-step transformation
const ast = parse(pythonCode)
const transformed = transform(ast)
const { code, usedRuntimeFunctions } = generate(transformed, {
  includeRuntime: true,
  runtimeImportPath: "pythonlib"
})

Runtime Library

python2ts uses pythonlib for Python standard library functions:

// Generated imports (grouped by module)
import { range, enumerate, len } from "pythonlib"
import { chain, combinations } from "pythonlib/itertools"
import { Counter } from "pythonlib/collections"

pythonlib is automatically included as a dependency.

Type Hints Preserved

from typing import Optional, Callable, TypeVar

T = TypeVar('T')

def process(
    items: list[T],
    callback: Callable[[T, int], bool],
    default: Optional[str] = None
) -> dict[str, T]:
    ...

Becomes:

function process<T>(
  items: T[],
  callback: (arg0: T, arg1: number) => boolean,
  default_: string | null = null
): Record<string, T> {
  // ...
}

Requirements

  • Node.js >= 22.0.0
  • pythonlib — Python standard library for TypeScript (standalone runtime)

License

MIT