Convert JSON Schema to TypeScript Types
This feature allows you to convert a JSON Schema into a TypeScript type. The `FromSchema` utility type takes a JSON Schema object and infers the corresponding TypeScript type.
import { FromSchema } from 'json-schema-to-ts';
const userSchema = {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
age: { type: 'number' }
},
required: ['id', 'name']
} as const;
type User = FromSchema<typeof userSchema>;
const user: User = {
id: '123',
name: 'John Doe',
age: 30
};
Type Validation
This feature ensures that the TypeScript types derived from JSON Schema are validated at compile time, reducing runtime errors and improving code reliability.
import { FromSchema } from 'json-schema-to-ts';
const productSchema = {
type: 'object',
properties: {
productId: { type: 'string' },
price: { type: 'number' }
},
required: ['productId', 'price']
} as const;
type Product = FromSchema<typeof productSchema>;
const product: Product = {
productId: 'abc123',
price: 19.99
};