Skip to main content

py2ts

TypeScript type definitions from Python type hints

build status docs License: GPL v3

Features

  • Complex Types Support: Handle complex types such as enums and nested typed dictionaries.
  • Generic Types Support: Convert Python generics (TypeVars, bounded parameters, parametrized references) to TypeScript generics, including recursive and pydantic models.
  • Comprehensive Documentation: Access detailed documentation, including a Quickstart Guide and API Reference, to help you get started and understand the library's capabilities.

Installation

You can install Py2Ts from pypi directly using pip:

pip install python2ts

Quickstart

To generate TypeScript type definitions from Python type hints, use the generate_ts function from the py2ts.generate module. Here's an example:

from py2ts import generate_ts

class Person:
    name: str
    age: int

print(generate_ts(Person))

This will output the following TypeScript type definition:

export interface Person {
    name: string;
    age: number;
}

Please refer to the Quickstart Guide for more examples and detailed instructions.

Complex Types Support

Py2Ts supports complex types such as enums and nested typed dictionaries. Here's an example:

from __future__ import annotations
from enum import Enum
from typing_extensions import NotRequired
from typing import TypedDict
from py2ts import generate_ts

class Color(Enum):
    RED = 1
    GREEN = "green"
    BLUE = "blue"   

class Polygon(TypedDict):
    color: Color
    edges: NotRequired[int]
    children: list[Polygon] | None

ts = generate_ts(Polygon)

print(ts.full_str())

This will output the following TypeScript type definition:

export enum Color {
	RED = 1,
	GREEN = 'green',
	BLUE = 'blue',
}

export interface Polygon {
	color: Color;
	edges?: number;
	children: Array<Polygon> | null;
}

Generic Types Support

Py2Ts converts Python generic classes to TypeScript generics. Type parameters, bounds, parametrized references and recursive generics are supported:

from typing import Generic, TypeVar
from dataclasses import dataclass
from py2ts import generate_ts

T = TypeVar("T")

@dataclass
class Node(Generic[T]):
    value: T
    next: "Node[T] | None" = None

print(generate_ts(Node).full_str())
export interface Node<T> {
	value: T;
	next: Node<T> | null;
}

Bounded type parameters render as T extends ... (e.g. TypeVar("T", bound=str) becomes T extends string), and instantiations keep their type arguments (e.g. Node[str] becomes Node<string>). Pydantic generic models are supported as well. See the Supported Types page for details.

Metadata

Release files for python2ts 0.7.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python2ts 0.7.3
File Size Uploaded Provenance Actions
python2ts-0.7.3.tar.gz 216.2 kB With provenance Details for python2ts-0.7.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python2ts 0.7.3
File Interpreter ABI Platform Actions
python2ts-0.7.3-py3-none-any.whl Python 3 none any Details for python2ts-0.7.3-py3-none-any.whl

Total release size: 246.1 kB

Release files / python2ts-0.7.3.tar.gz

Download URL python2ts-0.7.3.tar.gz
Size 216.2 kB
Tags Source
SHA-256 checksum
How to use checksums
490de4fa6ef3e2159071abc6ddf733040f615bbb67d6dd87844ec15ac9e73d90
BLAKE2b-256 checksum
How to use checksums
02c7bccea7e81b738a90f5bfd8aa1bbe8de4df51fb4dfa0d6306e63a364b4f8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release files / python2ts-0.7.3-py3-none-any.whl

Download URL python2ts-0.7.3-py3-none-any.whl
Size 29.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b0772463a24decf072f238714217e90579962c33ed9e3b034055753faa796988
BLAKE2b-256 checksum
How to use checksums
c8d44ccf7491a43ee59aa55d317f12d473cdf367ab3cf2ddbe2c83ae4653725f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Security for python2ts 0.7.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for python2ts-0.7.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page