What is typescript-json-schema?
The typescript-json-schema npm package is a tool that generates JSON schemas from your TypeScript types. This can be particularly useful for validating JSON data, generating documentation, or ensuring consistency between your TypeScript code and JSON data structures.
What are typescript-json-schema's main functionalities?
Generate JSON Schema from TypeScript Interface
This feature allows you to generate a JSON schema from a TypeScript interface. The code sample demonstrates how to set up the schema generator, specify the TypeScript file, and generate the schema for a specific interface.
const TJS = require('typescript-json-schema');
// Settings for the schema generator
const settings = {
required: true
};
// Path to the TypeScript file
const files = ["./example.ts"];
// Generate the schema
const program = TJS.getProgramFromFiles(files);
const schema = TJS.generateSchema(program, "MyInterface", settings);
console.log(JSON.stringify(schema, null, 2));
Generate JSON Schema with Custom Settings
This feature allows you to customize the settings for the JSON schema generation. The code sample demonstrates how to use custom settings such as `noExtraProps` and `propOrder` to control the schema generation process.
const TJS = require('typescript-json-schema');
// Custom settings for the schema generator
const settings = {
required: true,
noExtraProps: true,
propOrder: true
};
// Path to the TypeScript file
const files = ["./example.ts"];
// Generate the schema
const program = TJS.getProgramFromFiles(files);
const schema = TJS.generateSchema(program, "MyInterface", settings);
console.log(JSON.stringify(schema, null, 2));
Generate JSON Schema for Multiple Interfaces
This feature allows you to generate JSON schemas for multiple TypeScript interfaces at once. The code sample demonstrates how to specify multiple interfaces and generate their schemas in a single operation.
const TJS = require('typescript-json-schema');
// Settings for the schema generator
const settings = {
required: true
};
// Path to the TypeScript file
const files = ["./example.ts"];
// Generate the schema
const program = TJS.getProgramFromFiles(files);
const schema = TJS.generateSchema(program, ["MyInterface1", "MyInterface2"], settings);
console.log(JSON.stringify(schema, null, 2));
Other packages similar to typescript-json-schema
typescript-to-json-schema
The typescript-to-json-schema package is another tool for generating JSON schemas from TypeScript types. It offers similar functionality to typescript-json-schema but with a different API and potentially different performance characteristics.
ts-json-schema-generator
The ts-json-schema-generator package is designed to generate JSON schemas from TypeScript types with a focus on simplicity and ease of use. It provides a straightforward API and is often used for quick schema generation tasks.
json-schema-to-typescript
The json-schema-to-typescript package works in the opposite direction, generating TypeScript types from JSON schemas. This can be useful for ensuring that your TypeScript code matches a given JSON schema, providing a complementary tool to typescript-json-schema.
typescript-json-schema
Generate json-schemas from your Typescript sources.
Features
- Compiles your Typescript program to get complete type information.
- Translates required properties, extends, annotation keywords, property initializers as defaults.
Usage
Node.js
- Install with
npm install typescript-json-schema -g
- Generate schema from a typescript type:
typescript-json-schema project/directory/tsconfig.json fully.qualified.type.to.generate
In case no tsconfig.json is available for your project, you can directly specify the .ts files (this in this case we use some built-in compiler presets):
- Generate schema from a typescript type:
typescript-json-schema project/directory/**/*.ts fully.qualified.type.to.generate
Background
Inspired and builds upon Typson, but typescript-json-schema is compatible with more recent Typescript versions. Also, since it uses the Typescript compiler internally, more advanced scenarios are possible.