Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

json-schema-typescript

SaleCycle45MIT0.1.3

Generate TypeScript interfaces from JSON schema files

readme

JSON schema to TypeScript Interfaces

Simple node module to generate Typescript interfaces from JSON Schema files. This package generates Typescript interfaces (.d.ts files) for a set of related JSON schema documents.

NOTE interface files are not generated for schemas which are not of type "object". At present all referenced schemas are only looked up against the glob pattern specified.

Installation

npm install json-schema-typescript

Usage

const jsonToTypescript = require('json-schema-typescript');

jsonToTypescript('path/to/schema/**/*.json', './outputDirectory')
    .then(files => console.log(files.length, 'TypeScript interfaces generated'))
    .catch(ex => console.error(ex));

jsonToTypescript(globPattern, [outputDirectory]) Argument one is a glob pattern to match against to load schemas. Argument two is an optional output directory. Defaults to current directory if not specified.

Returns a promise chain which completes with an array of file paths for the interfaces generated. You should implement a catch function to handle any errors.

Examples

For examples please see tests directory in the source code however an example output is shown below.

import {Name} from './Name';
import {Phone} from './Phone';
import {Address} from './Address';

export interface Customer {
  address?: Address;
  events?: Array<any|string|number>;
  id: string;
  interests?: Array<string>;
  name: Name;
  optIn?: {
    email?: boolean;
    phone?: boolean;
  };
  phone?: Phone;
  salutation?: string;
}

(The above example is generated from this schema)

Issues/Bugs

Please raise issues on github.