JSON Schema TypeScript utilities for the Interweb
Welcome to schema-typescript! This project provides robust tools for handling JSON schemas and converting them to TypeScript interfaces with ease and efficiency.
-
🔧 JSON Schema to TypeScript: Convert JSON schemas into TypeScript interfaces automatically.
-
📦 Modular: Designed to be reusable with minimal dependencies.
-
🔍 Supports
$ref
and$defs
: Fully supports JSON Schema references, allowing you to define complex schemas modularly. -
🐕 Multiple Entities Handling: Handles arrays of defined types, such as multiple dogs or cats, seamlessly in your schemas.
To get started with schema-typescript, simply run:
npm install schema-typescript
Here's a quick example to show you how to convert a JSON schema into TypeScript interfaces:
import { generateTypeScript } from 'schema-typescript';
const schema = {
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"firstName": { "type": "string" },
"pets": {
"type": "array",
"items": { "$ref": "#/$defs/pet" }
}
},
"required": ["firstName", "pets"],
"$defs": {
"pet": {
"type": "object",
"properties": {
"name": { "type": "string" },
"type": { "type": "string" }
},
"required": ["name", "type"]
}
}
};
console.log(generateTypeScript(schema));
// OUTPUT:
interface Pet {
name: string;
type: string;
}
interface Person {
firstName: string;
pets: Pet[];
}
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
schema-typescript
might not work perfectly for all JSON schemas yet. We value your feedback and contributions to make it better. If you encounter any issues or have suggestions for improvements, please let us know.
Distributed under the MIT License. See `LICENSE` for more information.