Generate JSON schema from your TypeScript sources with extensive customization options
npx @tessl/cli install tessl/npm-ts-json-schema-generator@2.4.0ts-json-schema-generator is a powerful TypeScript library that generates JSON schemas from TypeScript type definitions. It serves as both a command-line tool and a programmatic library, offering extensive customization options for advanced TypeScript features including generics, conditional types, mapped types, and more.
npm install ts-json-schema-generatorimport { createGenerator, Config, SchemaGenerator } from "ts-json-schema-generator";For CommonJS:
const { createGenerator } = require("ts-json-schema-generator");For advanced usage (custom parsers/formatters):
import {
createGenerator,
createParser,
createFormatter,
createProgram,
SchemaGenerator,
SubTypeFormatter,
SubNodeParser,
MutableParser,
MutableTypeFormatter,
BaseType,
Context
} from "ts-json-schema-generator";import { createGenerator } from "ts-json-schema-generator";
// Simple usage
const config = {
path: "path/to/source/file.ts",
tsconfig: "path/to/tsconfig.json",
type: "MyInterface", // or "*" for all types
};
const schema = createGenerator(config).createSchema(config.type);
console.log(JSON.stringify(schema, null, 2));ts-json-schema-generator uses a multi-stage architecture:
The library supports extensive customization through:
Core functionality for generating JSON schemas from TypeScript types with extensive configuration options.
function createGenerator(config: Config): SchemaGenerator;
interface Config {
path?: string;
type?: string;
tsconfig?: string;
expose?: "all" | "none" | "export";
jsDoc?: "none" | "extended" | "basic";
minify?: boolean;
sortProps?: boolean;
strictTuples?: boolean;
skipTypeCheck?: boolean;
encodeRefs?: boolean;
topRef?: boolean;
markdownDescription?: boolean;
extraTags?: string[];
additionalProperties?: boolean;
functions?: FunctionOptions;
schemaId?: string;
discriminatorType?: "json-schema" | "open-api";
}
type FunctionOptions = "fail" | "comment" | "hide";
type CompletedConfig = Config & typeof DEFAULT_CONFIG;Create and manage TypeScript programs with custom compiler options and file resolution.
function createProgram(config: CompletedConfig): ts.Program;Extensible parsing system that converts TypeScript AST nodes to internal type representations.
function createParser(
program: ts.Program,
config: CompletedConfig,
augmentor?: ParserAugmentor
): NodeParser;
type ParserAugmentor = (parser: MutableParser) => void;
interface NodeParser {
createType(node: ts.Node, context: Context, reference?: ReferenceType): BaseType;
}
interface MutableParser {
addNodeParser(parser: SubNodeParser): MutableParser;
}Convert internal type representations to JSON schema definitions with customizable formatters.
function createFormatter(
config: CompletedConfig,
augmentor?: FormatterAugmentor
): TypeFormatter;
type FormatterAugmentor = (
formatter: MutableTypeFormatter,
circularReferenceTypeFormatter: CircularReferenceTypeFormatter
) => void;
interface TypeFormatter {
getDefinition(type: BaseType): Definition;
getChildren(type: BaseType): BaseType[];
}
interface MutableTypeFormatter {
addTypeFormatter(formatter: SubTypeFormatter): MutableTypeFormatter;
}Command-line interface with comprehensive options for batch processing and automation.
npx ts-json-schema-generator --path 'src/**/*.ts' --type 'MyType'Create custom parsers and formatters to handle special TypeScript constructs or generate custom JSON schema formats.
interface SubNodeParser extends NodeParser {
supportsNode(node: ts.Node): boolean;
}
interface SubTypeFormatter extends TypeFormatter {
supportsType(type: BaseType): boolean;
}import type { JSONSchema7 } from "json-schema";
type Schema = JSONSchema7;
type Definition = JSONSchema7;abstract class BaseType {
abstract getId(): string;
getName(): string;
}class Context {
constructor(reference?: ts.Node);
pushArgument(argumentType: BaseType): void;
pushParameter(parameterName: string): void;
setDefault(parameterName: string, argumentType: BaseType): void;
getCacheKey(): string;
getArgument(parameterName: string): BaseType;
getParameters(): readonly string[];
getArguments(): readonly BaseType[];
getReference(): ts.Node | undefined;
}class BaseError extends Error {
format(): string;
}
class UnknownNodeError extends BaseError {}
class UnknownTypeError extends BaseError {}
class RootlessError extends BaseError {}
class MultipleDefinitionsError extends BaseError {}
class LogicError extends BaseError {}
class ExpectationFailedError extends BaseError {}
class JsonTypeError extends BaseError {}
class DefinitionError extends BaseError {}
class BuildError extends BaseError {}
class UnhandledError extends BaseError {}