Quickstart Guide¶
This guide will help you get started with converting Python types to TypeScript types using the provided functions.
Installation¶
First, ensure you have the necessary dependencies installed. You can install them using pip:
pip install git+https://github.com/semohr/py2ts.git
Usage¶
To convert a Python type to a TypeScript type, use the generate_ts function. This function will recursively convert the type and its arguments to a tree of typescript types.
Examples¶
You may convert simple types like but also more complex types. For a list of supported types, see here.
from typing import TypedDict
from py2ts import generate_ts
# Primitive types
ts_int = generate_ts(int)
print(ts_int)
number
# Derived types
ts_list = generate_ts(list[int])
print(ts_list)
Array<number>
# Complex types
class Person(TypedDict):
name: str
age: int
class House(TypedDict):
address: str
owner: Person
ts_house = generate_ts(House)
print(ts_house)
export interface House {
address: string;
owner: Person;
}
from py2ts.data import TSInterface
# You may als generate the full type definition included nested types
assert isinstance(ts_house, TSInterface)
print(ts_house.full_str())
export interface Person {
name: string;
age: number;
}
export interface House {
address: string;
owner: Person;
}
This guide will help you get started with converting Python types to TypeScript types using the provided functions.