{"id":13795929,"url":"https://github.com/phillipdupuis/pydantic-to-typescript","last_synced_at":"2025-05-15T01:07:09.178Z","repository":{"id":37932651,"uuid":"266360405","full_name":"phillipdupuis/pydantic-to-typescript","owner":"phillipdupuis","description":"CLI Tool for converting pydantic models into typescript definitions","archived":false,"fork":false,"pushed_at":"2024-11-22T04:21:23.000Z","size":97,"stargazers_count":340,"open_issues_count":29,"forks_count":52,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-14T00:57:23.300Z","etag":null,"topics":["converting-pydantic-models","fastapi","python","type-hints","typescript","typescript-definitions","validation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phillipdupuis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-23T15:06:42.000Z","updated_at":"2025-04-11T14:16:14.000Z","dependencies_parsed_at":"2024-01-13T10:41:32.112Z","dependency_job_id":"ca98098c-05a1-44d1-b36f-1378eec89e30","html_url":"https://github.com/phillipdupuis/pydantic-to-typescript","commit_stats":{"total_commits":22,"total_committers":6,"mean_commits":"3.6666666666666665","dds":0.5454545454545454,"last_synced_commit":"83ec9e9d6790daaa280c03f5e467b438891c0ed4"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phillipdupuis%2Fpydantic-to-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phillipdupuis%2Fpydantic-to-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phillipdupuis%2Fpydantic-to-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phillipdupuis%2Fpydantic-to-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phillipdupuis","download_url":"https://codeload.github.com/phillipdupuis/pydantic-to-typescript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254254041,"owners_count":22039792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["converting-pydantic-models","fastapi","python","type-hints","typescript","typescript-definitions","validation"],"created_at":"2024-08-03T23:01:04.194Z","updated_at":"2025-05-15T01:07:04.153Z","avatar_url":"https://github.com/phillipdupuis.png","language":"Python","funding_links":[],"categories":["Commands"],"sub_categories":[],"readme":"# pydantic-to-typescript\n\n[![PyPI version](https://badge.fury.io/py/pydantic-to-typescript.svg)](https://badge.fury.io/py/pydantic-to-typescript)\n[![CI/CD](https://github.com/phillipdupuis/pydantic-to-typescript/actions/workflows/cicd.yml/badge.svg)](https://github.com/phillipdupuis/pydantic-to-typescript/actions/workflows/cicd.yml)\n[![Coverage Status](https://coveralls.io/repos/github/phillipdupuis/pydantic-to-typescript/badge.svg?branch=master)](https://coveralls.io/github/phillipdupuis/pydantic-to-typescript?branch=master)\n\nA simple CLI tool for converting pydantic models into typescript interfaces.\nIt supports all versions of pydantic, with polyfills for older versions to ensure that the resulting typescript definitions are stable and accurate.\n\nUseful 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.\n\nThis tool requires that you have the lovely json2ts CLI utility installed.\nInstructions can be found here: https://www.npmjs.com/package/json-schema-to-typescript\n\n### Installation\n\n```bash\n$ pip install pydantic-to-typescript\n```\n\n### Pydantic V2 support\n\nIf you are encountering issues with `pydantic\u003e2`, it is most likely because you're using an old version of `pydantic-to-typescript`.\nRun `pip install 'pydantic-to-typescript\u003e2'` and/or add `pydantic-to-typescript\u003e=2` to your project requirements.\n\n### CI/CD\n\nYou can now use `pydantic-to-typescript` to automatically validate and/or update typescript definitions as part of your CI/CD pipeline.\n\nThe github action can be found here: https://github.com/marketplace/actions/pydantic-to-typescript.\nThe available inputs are documented here: https://github.com/phillipdupuis/pydantic-to-typescript/blob/master/action.yml.\n\n### CLI\n\n| Prop                            | Description                                                                                                                                                                                                                             |\n| :------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| \u0026#8209;\u0026#8209;module            | name or filepath 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.                                            |\n| \u0026#8209;\u0026#8209;output            | name of the file the typescript definitions should be written to. Ex: './frontend/apiTypes.ts'                                                                                                                                          |\n| \u0026#8209;\u0026#8209;exclude           | name of a pydantic model which should be omitted from the resulting typescript definitions. This option can be defined multiple times, ex: `--exclude Foo --exclude Bar` to exclude both the Foo and Bar models from the output.        |\n| \u0026#8209;\u0026#8209;json2ts\u0026#8209;cmd | optional, the command used to invoke json2ts. The default is 'json2ts'. Specify this if you have it installed locally (ex: 'yarn json2ts') or if the exact path to the executable is required (ex: /myproject/node_modules/bin/json2ts) |\n\n---\n\n### Usage\n\nDefine your pydantic models (ex: /backend/api.py):\n\n```python\nfrom fastapi import FastAPI\nfrom pydantic import BaseModel\nfrom typing import List, Optional\n\napi = FastAPI()\n\nclass LoginCredentials(BaseModel):\n    username: str\n    password: str\n\nclass Profile(BaseModel):\n    username: str\n    age: Optional[int]\n    hobbies: List[str]\n\nclass LoginResponseData(BaseModel):\n    token: str\n    profile: Profile\n\n@api.post('/login/', response_model=LoginResponseData)\ndef login(body: LoginCredentials):\n    profile = Profile(**body.dict(), age=72, hobbies=['cats'])\n    return LoginResponseData(token='very-secure', profile=profile)\n```\n\nExecute the command for converting these models into typescript definitions, via:\n\n```bash\n$ pydantic2ts --module backend.api --output ./frontend/apiTypes.ts\n```\n\nor:\n\n```bash\n$ pydantic2ts --module ./backend/api.py --output ./frontend/apiTypes.ts\n```\n\nor:\n\n```python\nfrom pydantic2ts import generate_typescript_defs\n\ngenerate_typescript_defs(\"backend.api\", \"./frontend/apiTypes.ts\")\n```\n\nThe models are now defined in typescript...\n\n```ts\n/* tslint:disable */\n/**\n/* This file was automatically generated from pydantic models by running pydantic2ts.\n/* Do not modify it by hand - just update the pydantic models and then re-run the script\n*/\n\nexport interface LoginCredentials {\n  username: string;\n  password: string;\n}\nexport interface LoginResponseData {\n  token: string;\n  profile: Profile;\n}\nexport interface Profile {\n  username: string;\n  age?: number;\n  hobbies: string[];\n}\n```\n\n...and can be used in your typescript code with complete confidence.\n\n```ts\nimport { LoginCredentials, LoginResponseData } from \"./apiTypes.ts\";\n\nasync function login(\n  credentials: LoginCredentials,\n  resolve: (data: LoginResponseData) =\u003e void,\n  reject: (error: string) =\u003e void\n) {\n  try {\n    const response: Response = await fetch(\"/login/\", {\n      method: \"POST\",\n      headers: { \"Content-Type\": \"application/json\" },\n      body: JSON.stringify(credentials),\n    });\n    const data: LoginResponseData = await response.json();\n    resolve(data);\n  } catch (error) {\n    reject(error.message);\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphillipdupuis%2Fpydantic-to-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphillipdupuis%2Fpydantic-to-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphillipdupuis%2Fpydantic-to-typescript/lists"}