JSON Schema to TypeScript Type Inferer

3.1.1 · active · verified Sun Apr 19

json-schema-to-ts is a TypeScript library designed to infer static TypeScript types directly from existing JSON schemas. This eliminates the need for manual type duplication, mitigating potential bugs and reducing maintenance overhead in projects that rely on JSON schemas for runtime data validation. The current stable version is 3.1.1, with a release cadence that is quite active, typically seeing minor or patch updates on a monthly basis. A key differentiator from libraries like Zod, Yup, or Runtypes is its focus purely on type inference from the established JSON Schema standard, rather than providing an integrated validation and schema definition system. This makes it an ideal choice for development environments where JSON schemas are already a primary source of truth for API specifications (e.g., OpenAPI) or data contracts, enabling seamless integration of existing schemas for robust static type checking.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a JSON schema and infer its TypeScript type using `FromSchema`, along with how to instantiate an object conforming to that type.

import { FromSchema } from 'json-schema-to-ts';

const userSchema = {
  type: 'object',
  properties: {
    id: { type: 'string', format: 'uuid' },
    name: { type: 'string' },
    email: { type: 'string', format: 'email' },
    age: { type: 'integer', minimum: 0 },
    isActive: { type: 'boolean', default: true },
    roles: { type: 'array', items: { enum: ['admin', 'editor', 'viewer'] } },
  },
  required: ['id', 'name', 'email', 'age'],
  additionalProperties: false,
} as const;

type User = FromSchema<typeof userSchema>;

const newUser: User = {
  id: 'a1b2c3d4-e5f6-7890-1234-567890abcdef',
  name: 'Jane Doe',
  email: 'jane.doe@example.com',
  age: 30,
  // isActive will be inferred as boolean and optional due to default, but is required by v3 behavior for defaulted properties.
  roles: ['editor']
};

console.log(newUser);
// Expected output: { id: '...', name: 'Jane Doe', email: 'jane.doe@example.com', age: 30, roles: [ 'editor' ] }

view raw JSON →