npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@barusu/typescript-json-schema

v0.3.1

Published

typescript-json-schema generates JSON Schema files from you Typescript sources

Readme

typescript-json-schema

npm version

Generate json-schemas from your Typescript sources.

I sincerely suggest you use the original repository, as this repository/package is forked for demanding of myself (study and customization).

Peer Commit Id: https://github.com/YousefED/typescript-json-schema/commit/20a03a2d2fe81bea56a895cee7975f87fbf480f8

Features

  • 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.

Usage

Programmatic use

import { resolve } from 'path'
import * as TJS from '@barusu/typescript-json-schema'

// optionally pass argument to schema generator
const settings: TJS.PartialArgs = {
  ref: false,
  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)

// generator can be also reused to speed up generating the schema if usecase allows:
const schemaWithReusedGenerator = TJS.generateSchema(program, 'MyType', settings, [], generator)

// all symbols
const symbols = generator.getUserSymbols()

// Get symbols for different types from generator.
generator.getSchemaForSymbol('MyType')
generator.getSchemaForSymbol('AnotherType')
// 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.
generator.getSchemaForSymbol(symbolList[1].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.

Annotations

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": "http://json-schema.org/draft-07/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.

Example:

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[];
}

Translation:

{
    "$ref": "#/definitions/ShapesData",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "Shape": {
            "properties": {
                "sizes": {
                    "description": "Specify individual fields in items.",
                    "items": {
                        "minimum": 0,
                        "type": "integer"
                    },
                    "type": "array"
                },
                "emails": {
                    "description": "Or specify a JSON spec:",
                    "items": {
                        "format": "email",
                        "type": "string"
                    },
                    "type": "array"
                }
            },
            "type": "object"
        }
    }
}

This same syntax can be used for contains and additionalProperties.

integer type alias

If you create a type alias integer for number it will be mapped to the integer type in the generated JSON schema.

Example:

type integer = number;
interface MyObject {
    n: integer;
}

Note: this feature doesn't work for generic types & array types, it mainly works in very simple cases.

require a variable from a file

(for requiring typescript files is needed to set argument tsNodeRegister to true)

When you want to import for example an object or an array into your property defined in annotation, you can use require.

Example:

export interface InnerData {
    age: number;
    name: string;
    free: boolean;
}

export interface UserData {
    /**
     * Specify required object
     *
     * @examples require("./example.ts").example
     */
    data: InnerData;
}

file example.ts

export const example: InnerData[] = [{
  age: 30,
  name: "Ben",
  free: false
}]

Translation:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "data": {
            "description": "Specify required object",
            "examples": [
                {
                    "age": 30,
                    "name": "Ben",
                    "free": false
                }
            ],
            "type": "object",
            "properties": {
                "age": { "type": "number" },
                "name": { "type": "string" },
                "free": { "type": "boolean" }
            },
            "required": ["age", "free", "name"]
        }
    },
    "required": ["data"],
    "type": "object"
}

Also you can use require(".").example, which will try to find exported variable with name 'example' in current file. Or you can use require("./someFile.ts"), which will try to use default exported variable from 'someFile.ts'.

Note: For examples a required variable must be an array.