Convert python typehints to typescript interfaces
Project description
py2ts
TypeScript type definitions from Python type hints
Features
- Complex Types Support: Handle complex types such as enums and nested typed dictionaries.
- 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;
}
Project details
Release history Release notifications | RSS feed
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.6.1.tar.gz.
File metadata
- Download URL: python2ts-0.6.1.tar.gz
- Upload date:
- Size: 37.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6a2deb39b4bd50aa1723afa93d3a45a1921eb9ba506ea20433fa1b5ac8efe4e
|
|
| MD5 |
c1452eee267b34031a02ee8df7eaeb33
|
|
| BLAKE2b-256 |
ac5644919c9a70a6e94c37491fdf31dbd45971c3b633add199c84fe7060b4fe5
|
Provenance
The following attestation bundles were made for python2ts-0.6.1.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.6.1.tar.gz -
Subject digest:
c6a2deb39b4bd50aa1723afa93d3a45a1921eb9ba506ea20433fa1b5ac8efe4e - Sigstore transparency entry: 488028152
- Sigstore integration time:
-
Permalink:
semohr/py2ts@b250d45a6a9064f9c2ed9b6fa38cf2502ad1e6b6 -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/semohr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b250d45a6a9064f9c2ed9b6fa38cf2502ad1e6b6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file python2ts-0.6.1-py3-none-any.whl.
File metadata
- Download URL: python2ts-0.6.1-py3-none-any.whl
- Upload date:
- Size: 24.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd3cdc22e512b0b5fb8c0345a57e3befdfdd82e5d1889eb73c9546fe8018f5c3
|
|
| MD5 |
402d28a651c7022949120d698309a490
|
|
| BLAKE2b-256 |
90e3ba0e1e01d3b414917c9be17fca939acd971ef7db17561756a248ba98cca0
|
Provenance
The following attestation bundles were made for python2ts-0.6.1-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.6.1-py3-none-any.whl -
Subject digest:
cd3cdc22e512b0b5fb8c0345a57e3befdfdd82e5d1889eb73c9546fe8018f5c3 - Sigstore transparency entry: 488028162
- Sigstore integration time:
-
Permalink:
semohr/py2ts@b250d45a6a9064f9c2ed9b6fa38cf2502ad1e6b6 -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/semohr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b250d45a6a9064f9c2ed9b6fa38cf2502ad1e6b6 -
Trigger Event:
release
-
Statement type: