TypeScript to JSON Schema Generator

Convert TypeScript interfaces and types to JSON Schema (Draft 2020-12) for runtime validation

JSON Schema will appear here...

What is TypeScript to JSON Schema Generator?

JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. This tool converts TypeScript interface and type definitions into JSON Schema (Draft 2020-12), enabling runtime validation, documentation generation, and IDE integration for your TypeScript types.

How to Use

  1. Paste your TypeScript interface or type definitions into the left panel.
  2. Click "Generate" to produce the JSON Schema.
  3. Copy the output and use it with any JSON Schema validator.
  4. The schema uses $defs for referenced types and $ref for type references.

Why Use This Tool?

Supports interface and type alias definitions
Handles optional properties (?:) correctly
Parses union types and string literal unions as enums
Supports nested objects, arrays, and complex types
Generates JSON Schema Draft 2020-12 compliant output
Supports utility types: Partial, Required, Pick, Omit, Record

Tips & Best Practices

  • Use string literal unions for enum-like types: type Status = "active" | "inactive"
  • Optional properties (?) are not added to the "required" array
  • Date types are converted to string with format: "date-time"
  • Use $defs and $ref for reusable type definitions
  • Validate with AJV, jsonschema, or other JSON Schema validators

Frequently Asked Questions

What TypeScript features are supported?

This tool supports: interface and type definitions, optional properties (?:), primitive types (string, number, boolean, null, undefined), arrays (Type[] and Array<Type>), union types (|), string/numeric/boolean literals, nested objects, type references, and utility types (Partial, Required, Pick, Omit, Record).

How are union types handled?

Union types are converted to JSON Schema's anyOf. String literal unions (like "admin" | "user") are converted to enum arrays for cleaner output. Mixed unions use anyOf with the appropriate type schemas.

Why use $defs and $ref?

JSON Schema Draft 2020-12 uses $defs to define reusable schemas and $ref to reference them. This allows you to define types once and reference them multiple times, matching TypeScript's type reference semantics. The main schema uses $ref to point to the first defined type.

How do I validate data with the generated schema?

Use any JSON Schema validator. With AJV: import Ajv from "ajv"; const ajv = new Ajv(); const validate = ajv.compile(schema); const isValid = validate(data). If isValid is true, data conforms to your TypeScript types.

Related Tools