Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

json-schema-to-typescript-with-deps

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schema-to-typescript-with-deps

compile json schema to typescript typings

latest
Source
npmnpm
Version
0.0.1
Version published
Weekly downloads
20
-13.04%
Maintainers
1
Weekly downloads
 
Created
Source

json-schema-to-typescript-with-deps Build Status npm mit node

Compile JSON Schema to TypeScript typings.

Note: This is a fork of json-schema-to-typescript whose intention is to support the use dependencies within JSON schemas. Right now the fork supports them when used in combination with oneOf and may not actually support other use-cases currently.

Example

Check out the live demo.

Input:

{
  "title": "Example Schema",
  "type": "object",
  "properties": {
    "occupation": {
      "enum": ["farmer", "other"]
    },
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "description": "Age in years",
      "type": "integer",
      "minimum": 0
    },
    "hairColor": {
      "enum": ["black", "brown", "blue"],
      "type": "string"
    }
  },
  "dependencies": {
    "occupation": {
      "oneOf": [
        {
          "properties": {
            "occupation": {
              "enum": ["farmer"]
            }
          },
          {
            "required": ["jobDescription"],
            "properties": {
              "occupation": {
                "enum": ["other"]
              },
              "jobDescription": {
                "type": "string"
              }
            }
          }
        }
      ]
    }
  },
  "additionalProperties": false,
  "required": ["firstName", "lastName", "occupation"]
}

Output:

export type ExampleSchema = ExampleSchema1 & {
  firstName: string;
  lastName: string;
  occupation: "farmer" | "other"
  /**
   * Age in years
   */
  age?: number;
  hairColor?: "black" | "brown" | "blue";
}

export type ExampleSchema1 = 
  | {
      occupation?: "farmer"
    }
  | {
      occupation?: "other",
      jobDescription: string
    }

Installation

npm install json-schema-to-typescript-with-deps

Usage

json-schema-to-typescript is easy to use via the CLI, or programmatically.

CLI

First make the CLI available using one of the following options:

# install locally, then use `npx json2ts`
npm install json-schema-to-typescript-with-deps

# or install globally, then use `json2ts`
npm install json-schema-to-typescript-with-deps --global

# or install to npm cache, then use `npx --package=json-schema-to-typescript-with-deps json2ts`
# (you don't need to run an install command first)

Then, use the CLI to convert JSON files to TypeScript typings:

cat foo.json | json2ts > foo.d.ts
# or
json2ts foo.json > foo.d.ts
# or
json2ts foo.yaml foo.d.ts
# or
json2ts --input foo.json --output foo.d.ts
# or
json2ts -i foo.json -o foo.d.ts
# or (quote globs so that your shell doesn't expand them)
json2ts -i 'schemas/**/*.json'
# or
json2ts -i schemas/ -o types/

You can pass any of the options described below (including style options) as CLI flags. Boolean values can be set to false using the no- prefix.

# generate code for definitions that aren't referenced
json2ts -i foo.json -o foo.d.ts --unreachableDefinitions
# use single quotes and disable trailing semicolons
json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi

API

See the original repo for more usage and API examples.

Keywords

json

FAQs

Package last updated on 06 May 2025

Did you know?

Socket

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.

Install

Related posts