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

Uploaded Python 3

File details

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

File metadata

  • Download URL: python2ts-0.7.0.tar.gz
  • Upload date:
  • Size: 210.8 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.0.tar.gz
Algorithm Hash digest
SHA256 00d267c7f1447b9e6a7adb704ee92a8a6f602fd62083b56830a7f9ce3872367b
MD5 9247d2f23f5f570d2875ffc210a5c939
BLAKE2b-256 0c1aa78d79d51ff0f7be6bd25be3fcf0ee4eaecf8eec1fbe146e289171df1212

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: python2ts-0.7.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff814e6138175095b7fcb499660fc1c672f7605cd24b1ef264ec03a9aae6fac7
MD5 d29f0afd0f8e52122d072b810a2cfe15
BLAKE2b-256 963f943a0adb83f75d33b254cbdf86ef28c47fd15d9831f83fc3287350bf04e7

See more details on using hashes here.

Provenance

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

0.7.1

2 files

This release

0.7.0 This release

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