Generate json-schema from your Typescript sources

License: BSD-3-Clause

Language: TypeScript


npm version Build Status

Generate json-schemas from your Typescript sources.


  • Compiles your Typescript program to get complete type information.
  • Translates required properties, extends, annotation keywords, property initializers as defaults. You can find examples for these features in the test examples.


Command line

  • Install with npm install typescript-json-schema -g
  • Generate schema from a typescript type: typescript-json-schema project/directory/tsconfig.json TYPE

To generate files for only some types in tsconfig.json specify filenames or globs with the --include option. This is especially useful for large projects.

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" TYPE

The TYPE can either be a single, fully qualified type or "*" to generate the schema for all types.

Usage: typescript-json-schema <path-to-typescript-files-or-tsconfig> <type>

  --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]
  --useTypeOfKeyword    Use `typeOf` keyword ( for functions.  [boolean] [default: false]
  --out, -o             The output file, defaults to using stdout
  --validationKeywords  Provide additional validation keywords to include            [array]   [default: []]
  --include             Further limit tsconfig to include only matching files        [array]   [default: []]
  --ignoreErrors        Generate even if the program has errors.                     [boolean] [default: false]
  --excludePrivate      Exclude private members from the schema                      [boolean] [default: false]
  --uniqueNames         Use unique names for type symbols.                           [boolean] [default: false]
  --rejectDateType      Rejects Date fields in type definitions.                     [boolean] [default: false]
  --id                  Set schema id.                                               [string]  [default: ""]
  --defaultNumberType   Default number type.                                         [choices: "number", "integer"] [default: "number"]

Programmatic use

import {resolve} from "path";

import * as TJS from "typescript-json-schema";

// optionally pass argument to schema generator
const settings: TJS.PartialArgs = {
    required: true

// optionally pass ts compiler options
const compilerOptions: TJS.CompilerOptions = {
    strictNullChecks: true

// optionally pass a base path
const basePath = "./my-dir";

const program = TJS.getProgramFromFiles([resolve("my-file.ts")], compilerOptions, basePath);

// 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.
// In larger projects type names may not be unique,
// while unique names may be enabled.
const settings: TJS.PartialArgs = {
    uniqueNames: true

const generator = TJS.buildGenerator(program, settings);

// A list of all types of a given name can then be retrieved.
const symbolList = generator.getSymbols("MyType");

// Choose the appropriate type, and continue with the symbol's unique name.

// Also it is possible to get a list of all symbols.
const fullSymbolList = generator.getSymbols();

getSymbols('<SymbolName>') and getSymbols() return an array of SymbolRef, which is of the following format:

type SymbolRef = {
  name: string;
  typeName: string;
  fullyQualifiedName: string;
  symbol: ts.Symbol;

getUserSymbols and getMainFileSymbols return an array of string.


The schema generator converts annotations to JSON schema properties.

For example

export interface Shape {
     * The size of the shape.
     * @minimum 0
     * @TJS-type integer
    size: number;

will be translated to

    "$ref": "#/definitions/Shape",
    "$schema": "",
    "definitions": {
        "Shape": {
            "properties": {
                "size": {
                    "description": "The size of the shape.",
                    "minimum": 0,
                    "type": "integer"
            "type": "object"

Note that we needed to use @TJS-type instead of just @type because of an issue with the typescript compiler.

You can also override the type of array items, either listing each field in its own annotation or one annotation with the full JSON of the spec (for special cases). This replaces the item types that would have been inferred from the TypeScript type of the array elements.


export interface ShapesData {
     * Specify individual fields in items.
     * @items.type integer
     * @items.minimum 0
    sizes: number[];

     * Or specify a JSON spec:
     * @items {"type":"string","format":"email"}
    emails: string[];


    "$ref": "#/definitions/ShapesData",
    "$schema": "",
    "definitions": {
        "Shape": {
            "properties": {
                "sizes": {
                    "description": "Specify individual fields in items.",
                    "items": {
                        "minimum": 0,
                        "type": "integer"
                    "type": "array"
                "emails": {
                    "description": "Specify individual fields in items.",
                    "items": {
                        "format": "email",
                        "type": "string"
                    "type": "array"
            "type": "object"

This same syntax can be used for contains and additionalProperties.


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. If you are looking for a library that uses the AST instead of the type hierarchy and therefore better support for type aliases, have a look at vega/ts-json-schema-generator.


npm run debug -- test/programs/type-alias-single/main.ts --aliasRefs true MyString

And connect via the debugger protocol.

Project Statistics

Sourcerank 13
Repository Size 943 KB
Stars 1,015
Forks 155
Watchers 20
Open issues 75
Dependencies 180
Contributors 63
Tags 55
Last updated
Last pushed

Top Contributors See all

Dominik Moritz Yousef Otger Rogla Mark Probst Marc Prud'hommeaux Alex Zaslavsky Ryan Petrich Noah Chen Brett Jurgens Robert Massaioli Youying Lin Sam J Rafael Cossovan Fabian Pirklbauer Forbes Lindesay Dobes Vandermeer 尹良灿 Marcus Riemer jfurler benny-medflyt

Packages Referencing this Repo

typescript-json-schema generates JSON Schema files from your Typescript sources
Latest release 0.37.0 - Published - 1.02K stars
typescript-json-schema generates JSON Schema files from your Typescript sources
Latest release 0.36.0 - Published - 1.02K stars
typescript-json-schema generates JSON Schema files from your Typescript sources
Latest release 0.32.0 - Updated - 1.02K stars
typescript-json-schema generates JSON Schema files from your Typescript sources
Latest release 0.27.0-updatets - Published - 1.02K stars
typescript-json-schema generates JSON Schema files from your Typescript sources
Latest release 0.21.0 - Updated - 1.02K stars
typescript-json-schema generates JSON Schema files from your Typescript sources
Latest release 0.42.0 - Updated - 1.02K stars

Recent Tags See all

v0.42.0 January 07, 2020
v0.41.0 December 06, 2019
v0.40.0 August 21, 2019
v0.39.0 July 24, 2019
v0.38.3 June 03, 2019
v0.38.2 May 31, 2019
v0.38.1 May 25, 2019
v0.38.0 May 15, 2019
v0.37.0 April 02, 2019
v0.36.0 February 15, 2019
v0.35.0 February 09, 2019
v0.34.0 December 27, 2018
v0.33.0 October 29, 2018
v0.32.0 August 24, 2018
v0.31.0 July 31, 2018

Interesting Forks See all

Generate json-schema from your Typescript sources
TypeScript - BSD-3-Clause - Last pushed - 1 stars
Generate json-schema from your Typescript sources
TypeScript - Last pushed - 1 stars

Something wrong with this page? Make a suggestion

Last synced: 2019-12-06 00:50:41 UTC

Login to resync this repository