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.1.tar.gz (211.5 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.1-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python2ts-0.7.1.tar.gz
  • Upload date:
  • Size: 211.5 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.1.tar.gz
Algorithm Hash digest
SHA256 569bdbead395279e522d9f6d19a0aaf9a7e9573830c928674a5da20529b928c9
MD5 556e894acf1a8be318c309e0069d8111
BLAKE2b-256 cdf770d8d722c12f18f2311090237bdda0f5b2766474959ad7fbd980d4eccfeb

See more details on using hashes here.

Provenance

The following attestation bundles were made for python2ts-0.7.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: python2ts-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 28.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d11317bb244a83e585e952df858544af3e92d5af7b8d93551ddb86b181668bea
MD5 e32d570c6438f19275030344b4d80a3a
BLAKE2b-256 d05b1c91e91b3a1c0b41852986508381896ec6e598804a0b7af0132afcfe1427

See more details on using hashes here.

Provenance

The following attestation bundles were made for python2ts-0.7.1-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

0.7.2

2 files

This release

0.7.1 This release

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