
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
typescript-json-schema
Advanced tools
typescript-json-schema generates JSON Schema files from your Typescript sources
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.
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));
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.
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.
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.
Generate json-schemas from your Typescript sources.
npm install typescript-json-schema -g
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):
typescript-json-schema "project/directory/**/*.ts" fully.qualified.type.to.generate
Usage: node typescript-json-schema.js <path-to-typescript-files-or-tsconfig> <type>
Options:
--refs Create shared ref definitions. [boolean] [default: true]
--aliasRefs Create shared ref definitions for the type aliases. [boolean] [default: false]
--topRef Create a top-level ref definition. [boolean] [default: false]
--titles Creates titles in the output schema. [boolean] [default: false]
--defaultProps Create default properties definitions. [boolean] [default: false]
--noExtraProps Disable additional properties in objects by default. [boolean] [default: false]
--propOrder Create property order definitions. [boolean] [default: false]
--required Create required array for non-optional properties. [boolean] [default: false]
--strictNullChecks Make values non-nullable by default. [boolean] [default: false]
--out, -o The output file, defaults to using stdout
import {resolve} from "path";
import {CompilerOptions} from "typescript";
import * as TJS from "typescript-json-schema";
// optionally pass argument to schema generator
const settings: TJS.PartialArgs = {
generateRequired: true
};
// optionally pass ts compiler options
compilerOptions: CompilerOptions = {
strictNullChecks: true
}
const program = TJS.getProgramFromFiles([resolve("my-file.ts")], compilerOptions);
// We can either get the schema for one file and one type...
const schema = TJS.generateSchema(program, "MyType", settings);
// ... or a generator that lets us incrementally get more schemas
const generator = TJS.buildGenerator(program, settings);
// all symbols
const symbols = generator.getUserSymbols();
// Get symbols for different types from generator.
generator.getSchemaForSymbol("MyType");
generator.getSchemaForSymbol("MyType");
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.
FAQs
typescript-json-schema generates JSON Schema files from your Typescript sources
The npm package typescript-json-schema receives a total of 462,259 weekly downloads. As such, typescript-json-schema popularity was classified as popular.
We found that typescript-json-schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.