Type Definition Generator
Prerequisites & Dependencies
- Node.js 18+ or Python 3.10+
npm i -g quicktypeorpip install pydantic mypyfor code generation- Sample untyped JSON payloads or Python dicts to generate types from
- Optional:
npm i ajvfor JSON Schema validation,pip install marshmallowfor Python
Execution Steps
- Collect a representative set of JSON examples (ideally 10–20) covering all branches, nulls, nested objects, and arrays
- Run a code generator:
- Quicktype:
quicktype input.json -l typescript→ produces.d.tsinterfaces with JSON Schema annotations - Pydantic/Marshmallow:
python generate.py→ producesclass User(BaseModel): ...or schema fields
- Quicktype:
- Review the generated types for correctness: verify union types, required/optional markers, enum values
- Apply the types in the project: import interfaces in TypeScript, use Pydantic models in Python APIs
- Run type checking:
npx tsc --noEmitormypy pipeline.pyto catch mismatches early - Iterate: add more JSON samples or adjust generator flags (
--enum-as-string,--just-types) until the output matches the project's typing style
# Quicktype: generate TypeScript interfaces from JSON
quicktype src/payloads/user.json -l typescript -o src/types/user.d.ts
# Pydantic example: from dict to typed model
from pydantic import BaseModel, EmailStr
class User(BaseModel):
id: int
name: str
email: EmailStr
is_active: bool = True
user = User(**{"id": 1, "name": "Alice", "email": "alice@example.com"})