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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python2ts-0.7.2.tar.gz (214.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python2ts-0.7.2-py3-none-any.whl (28.9 kB view details)

Uploaded Python 3

File details

Details for the file python2ts-0.7.2.tar.gz.

File metadata

  • Download URL: python2ts-0.7.2.tar.gz
  • Upload date:
  • Size: 214.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for python2ts-0.7.2.tar.gz
Algorithm Hash digest
SHA256 e2d70cd165aa1152342a037756887d1781f01a758e70994b67f4ff41b8d09b0c
MD5 e1a7d9f4e1265e4d3bc371721c4ed464
BLAKE2b-256 17c1798f4f89a58634695cec8bc46ac3fc0f6091415eb19e06446958a6fb1f68

See more details on using hashes here.

Provenance

The following attestation bundles were made for python2ts-0.7.2.tar.gz:

Publisher: release.yml on semohr/py2ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python2ts-0.7.2-py3-none-any.whl.

File metadata

  • Download URL: python2ts-0.7.2-py3-none-any.whl
  • Upload date:
  • Size: 28.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for python2ts-0.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 694a82a4cbded788649eb3c4a2329101fc129a340c353d2ec725856af95293fe
MD5 a41bc7dd1fe39f33ae018a6c764057af
BLAKE2b-256 d2cb1a86292dcff5c0c71288510ff4f0b5234a615e150854ca81d43837ec0798

See more details on using hashes here.

Provenance

The following attestation bundles were made for python2ts-0.7.2-py3-none-any.whl:

Publisher: release.yml on semohr/py2ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.7.2 This release

2 files

0.7.1

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

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