py2ts
TypeScript type definitions from Python type hints
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2d70cd165aa1152342a037756887d1781f01a758e70994b67f4ff41b8d09b0c
|
|
| MD5 |
e1a7d9f4e1265e4d3bc371721c4ed464
|
|
| BLAKE2b-256 |
17c1798f4f89a58634695cec8bc46ac3fc0f6091415eb19e06446958a6fb1f68
|
Provenance
The following attestation bundles were made for python2ts-0.7.2.tar.gz:
Publisher:
release.yml on semohr/py2ts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python2ts-0.7.2.tar.gz -
Subject digest:
e2d70cd165aa1152342a037756887d1781f01a758e70994b67f4ff41b8d09b0c - Sigstore transparency entry: 2783872895
- Sigstore integration time:
-
Permalink:
semohr/py2ts@b319c4852b8cc67876f50ec6b721249e582a388f -
Branch / Tag:
refs/tags/v0.7.2 - Owner: https://github.com/semohr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b319c4852b8cc67876f50ec6b721249e582a388f -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
694a82a4cbded788649eb3c4a2329101fc129a340c353d2ec725856af95293fe
|
|
| MD5 |
a41bc7dd1fe39f33ae018a6c764057af
|
|
| BLAKE2b-256 |
d2cb1a86292dcff5c0c71288510ff4f0b5234a615e150854ca81d43837ec0798
|
Provenance
The following attestation bundles were made for python2ts-0.7.2-py3-none-any.whl:
Publisher:
release.yml on semohr/py2ts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python2ts-0.7.2-py3-none-any.whl -
Subject digest:
694a82a4cbded788649eb3c4a2329101fc129a340c353d2ec725856af95293fe - Sigstore transparency entry: 2783873908
- Sigstore integration time:
-
Permalink:
semohr/py2ts@b319c4852b8cc67876f50ec6b721249e582a388f -
Branch / Tag:
refs/tags/v0.7.2 - Owner: https://github.com/semohr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b319c4852b8cc67876f50ec6b721249e582a388f -
Trigger Event:
release
-
Statement type: