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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for pydantic-to-typescript-1.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8be1a322c97f55f37434d773eabfafe19aeff8baf051dde319ea79534011e2a |
|
MD5 | 67b38e9ad4bf8ef9ee379790fc11b0cf |
|
BLAKE2b-256 | 5c577f7a9180c61d535a035356ea383bc7d64f7badbe45fe61df9e81b9f4c809 |
Close
Hashes for pydantic_to_typescript-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9aa4d909ae2584d78cfc3b4a3cb7cbd397711f034733a1d1f1e3aff1abf074e |
|
MD5 | 99606dabd65a07ebbfc1d8aeb3d3266e |
|
BLAKE2b-256 | d878f6592aa9f272d7cc581dd1569d35aa077abb6897d08f6f98e856472e222d |