Python Classes to TypeScript Interfaces

Paste Python classes and get TypeScript interfaces and types. TypedDict, dataclass, Pydantic v2 BaseModel, NamedTuple, Enum and plain annotated classes are read, with Optional, Literal, Union, list, dict and tuple mapped.

Use the tool

Why this tool exists

When a Python API feeds a TypeScript front end, the response shape lives in Python models and has to be copied into TypeScript by hand. The copy drifts as soon as a field changes.

The converter reads the class definitions as text, with a line-based parser rather than by running Python. Optional[X] and X | None become X | null, fields with defaults become optional, NotRequired and total=False become optional, Literal becomes a literal union, str-based enums become a union or an enum, and Pydantic alias values become the property names. The result was type-checked with tsc and compared with real Pydantic JSON output. Everything runs in your browser and nothing is uploaded.

How to use it

  1. Paste your Python models. Imports and other code around them are ignored.
  2. Choose how defaults, enums, dates and property names are treated.
  3. Copy the TypeScript into your front end project.

Frequently asked questions

Which Python features are supported?

Classes with annotations (TypedDict, dataclass, Pydantic BaseModel, NamedTuple, Enum and plain), the functional TypedDict form, Generic[T], type aliases (Name = Union[...], type X = ..., TypeAlias, NewType) and Annotated with Field(alias=...). Runtime-built models, validators, computed fields and metaclasses are not seen.

Why does a field with a default become optional?

A dataclass or Pydantic default means the field may be left out when you build the object. If you are describing a response, where every field is present, turn the option off.

How are int and float mapped?

Both become number. Decimal, UUID, datetime, date, bytes and paths become string, because that is how Pydantic and json serialise them, unless you pick Date for datetimes.

Does it import or run my code?

No. The text is parsed in your browser and never executed, so classes that need installed packages or have side effects are fine.