Skip to main content

Convert python typehints to typescript interfaces

Project description

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.
  • 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


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.6.1.tar.gz (37.2 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.6.1-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

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

Hashes for python2ts-0.6.1.tar.gz
Algorithm Hash digest
SHA256 c6a2deb39b4bd50aa1723afa93d3a45a1921eb9ba506ea20433fa1b5ac8efe4e
MD5 c1452eee267b34031a02ee8df7eaeb33
BLAKE2b-256 ac5644919c9a70a6e94c37491fdf31dbd45971c3b633add199c84fe7060b4fe5

See more details on using hashes here.

Provenance

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

Hashes for python2ts-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cd3cdc22e512b0b5fb8c0345a57e3befdfdd82e5d1889eb73c9546fe8018f5c3
MD5 402d28a651c7022949120d698309a490
BLAKE2b-256 90e3ba0e1e01d3b414917c9be17fca939acd971ef7db17561756a248ba98cca0

See more details on using hashes here.

Provenance

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

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page