Skip to content

crimson206/py-ts-types

Repository files navigation

Python-TypeScript Type Converter

A Python library for seamless type conversion between Python and TypeScript, facilitating interoperability in multi-language projects.

Features

  • Convert Python type annotations to TypeScript types
  • Convert TypeScript type definitions back to Python annotations
  • Support for:
    • Basic types (str, int, float, bool, None)
    • Complex types (List, Dict, Tuple, Union, Optional)
    • Literal types
    • Pydantic models to TypeScript interfaces
    • Type metadata preservation
    • Multi-line docstring conversion

Installation

pip install crimson-py-ts-types-beta

Usage & Examples

The following examples demonstrate key features and use cases of the library. Each notebook contains detailed code samples and explanations.

Type Conversion Examples

  1. Quick Start Guide
    Basic setup and overview of Python-TypeScript type conversion, including type mapping and bi-directional conversion examples.

  2. Positive Conversion (Python → TypeScript)
    Detailed explanation of converting Python type annotations to TypeScript, focusing on AST-based conversion process.

  3. Negative Conversion (TypeScript → Python)
    Implementation details of converting TypeScript types back to Python annotations without relying on TypeScript parser.

Model Conversion Examples

  1. Pydantic Model Interface Generation
    Examples of converting Pydantic models to TypeScript interfaces while preserving:
    • Field types and optionality
    • Documentation and descriptions
    • Default values
    • Custom metadata

Each notebook is self-contained and includes:

  • Detailed explanations of the concepts
  • Working code examples
  • Edge cases and their solutions
  • Complete input/output demonstrations