TypeScript to Pydantic converter

Convert TypeScript to Pydantic v2: interfaces, type aliases and enums become models, for example to share API types between a TypeScript frontend and a Python backend.

TypeScript

Pydantic

Waiting for input

What you get

How each result is checked

  1. Step 1: Your code is read by the real TypeScript compiler. Type errors are reported with the line number.
  2. Step 1: The schema is compiled with Ajv, the most widely used JSON Schema validator.
  3. Step 1: Round trip: the schema is turned back into TypeScript, and the compiler must prove that every original type and its twin accept exactly the same values.
  4. Step 2: The models are run with the real pydantic 2 in your browser (CPython compiled to WebAssembly), not just generated.
  5. Step 2: Your examples, plus valid and broken test values built from the schema, are validated by pydantic (strict JSON mode) and by a reference JSON Schema validator (Ajv). They must agree.
  6. Step 2: Where pydantic is documented to differ (listed in the notes, for example patternProperties), only those exact rules are relaxed when comparing; any other difference fails the check.

What can't convert exactly

  • Put each JSDoc comment on its own line above the property it describes. TypeScript does not attach a /** */ comment written after other code on the same line, so its rules would not apply; the result names any such comment.
  • Date, functions, bigint, Map and Set have no JSON form. Date becomes an ISO date-time string; the others are reported. Types using them are not part of the round-trip proof, and the result says which.
  • Generic types such as Page<T> have no single schema; they are included wherever they are used with concrete types (for example Page<Item>).
  • By default objects reject keys they do not declare (additionalProperties: false). Choose "Allow" if your data may carry extra keys.
  • "a: T | undefined" becomes an optional key, because undefined never appears in JSON.
  • Optional properties that are not nullable become `Omittable[X] = None`: they may be left out, but null is rejected, as in JSON Schema. Prefer plain `X | None = None`? Choose "Simple" in the options (the model then also accepts null, and the notes say so).
  • oneOf, if/then/else, not, dependentRequired, dependentSchemas and contains are checked by small validators in the generated module (oneOf: exactly one branch must match). A Union is left as it is when its branches cannot overlap.
  • patternProperties, propertyNames and minProperties/maxProperties on models are not checked; the notes name them when your schema uses them.
  • Checks use strict validation. In pydantic's default (lax) mode it also converts some inputs, for example "1" to 1.
  • The first run downloads Python and pydantic (about 6 MB compressed), once.

Questions

How is a two-step conversion checked?

Your input is converted to JSON Schema first, then to the target format. Each step has its own independent check, and both must pass. The checks of both steps are listed under every result.

Is my data uploaded?

No. Everything runs in your browser, in a background worker. Nothing is sent to a server.

How do I keep TypeScript and Python API types in sync?

Keep the TypeScript as the source of truth and regenerate the Pydantic v2 models here when it changes. The conversion of TypeScript types to Python is checked in two steps, so the models accept the same data as your types.

How do I convert TypeScript to Pydantic online?

Paste your TypeScript or open a file on the left (or click Example). The Pydantic appears on the right as you type, already checked. Copy it, or download it as a file. Nothing is uploaded.

Can the Pydantic output be wrong?

Every result is checked before it is shown (the checks are listed on this page). If a check fails, the page says "Review needed" and why, instead of presenting the output as correct, and anything that cannot convert exactly is named in the notes.