Skip to main content

Convert pydantic models to typescript interfaces

Project description

pydantic-to-typescript

A simple CLI tool for converting pydantic models into typescript interfaces. Useful for any scenario in which python and javascript applications are interacting, since it allows you to have a single source of truth for type definitions.

This tool requires that you have the lovely json2ts CLI utility installed. Instructions can be found here: https://www.npmjs.com/package/json-schema-to-typescript

Installation

$ pip install pydantic-to-typescript

CLI

Prop Description
‑‑module name of the python module you would like to convert. All the pydantic models within it will be converted to typescript interfaces. Discoverable submodules will also be checked. Ex: 'pydantic2ts.examples.pydantic_models'
‑‑output name of the file the typescript definitions should be written to. Ex: './frontend/apiTypes.ts'
‑‑json2ts‑cmd optional, the command used to invoke json2ts. The default is 'json2ts'. Specify this if you have it installed in a strange location and need to provide the exact path (ex: /myproject/node_modules/bin/json2ts)

Usage

pydantic2ts/examples/pydantic_models.py:

from pydantic import BaseModel, Extra
from enum import Enum
from typing import List, Dict


class NoExtraProps:
    extra = Extra.forbid


class Sport(str, Enum):
    football = 'football'
    basketball = 'basketball'


class Athlete(BaseModel):
    name: str
    age: int
    sports: List[Sport]
    Config = NoExtraProps


class Team(BaseModel):
    name: str
    sport: Sport
    athletes: List[Athlete]
    Config = NoExtraProps


class League(BaseModel):
    cities: Dict[str, Team]
    Config = NoExtraProps

Command-line:

$ pydantic2ts --module pydantic2ts.examples.pydantic_models --output output.ts

output.ts:

/* tslint:disable */
/**
/* This file was automatically generated from pydantic models by running pydantic2ts.
/* Do not modify it by hand - just update the pydantic models and then re-run the script
*/

export interface Athlete {
  name: string;
  age: number;
  sports: ("football" | "basketball")[];
}
export interface League {
  cities: {
    [k: string]: Team;
  };
}
export interface Team {
  name: string;
  sport: "football" | "basketball";
  athletes: Athlete[];
}

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

pydantic-to-typescript-1.0.4.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_to_typescript-1.0.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-to-typescript-1.0.4.tar.gz.

File metadata

  • Download URL: pydantic-to-typescript-1.0.4.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for pydantic-to-typescript-1.0.4.tar.gz
Algorithm Hash digest
SHA256 e7a2d02cedd78bf17f41ac89aa835c95251137cf39b911447fbf3d4b2cc0d58f
MD5 318aa2f6a5aa61f1486309d8fef682c9
BLAKE2b-256 57955a85a8c0a9a689d5ef05b276312a877c9bad1c9d5860480807ef3b4de6d1

See more details on using hashes here.

File details

Details for the file pydantic_to_typescript-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: pydantic_to_typescript-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for pydantic_to_typescript-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9f8f526b6953a2699ddb850e163b015f1433fa0ea2033762c218fce908999561
MD5 974121098674bfea9d41b0e450d6278b
BLAKE2b-256 8c5dcbe2cb84c3b3fd241b4b9833a8150c71d2c871b6c4fc521b9529068e42bf

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