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 git directly using pip:

pip install git+https://github.com/semohr/py2ts.git

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: python2ts-0.6.0.tar.gz
  • Upload date:
  • Size: 36.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for python2ts-0.6.0.tar.gz
Algorithm Hash digest
SHA256 59644ce84be111d747ee2cb86f5e10959753c760af55883134939228bda71eba
MD5 655a81d5818f49f11c6a06485d9cafe8
BLAKE2b-256 29b1675e3157307ac64d7b6afefb511f4ec495a85e63868fa1d1cdae6ae0b558

See more details on using hashes here.

File details

Details for the file python2ts-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: python2ts-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for python2ts-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 019646d0757683e94a63db9f57f53eb464b102cd23541996573d448d555ccf9d
MD5 3314803f3ba05cf9319522ba2a6ed433
BLAKE2b-256 e8ff3b6b480fe3e9d37a4f5ef846434b0950a212d8c77457269ab635c9e74bd1

See more details on using hashes here.

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