🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

json-schema-to-typescript

Package Overview
Dependencies
Maintainers
1
Versions
116
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schema-to-typescript

compile json schema to typescript typings

15.0.4
latest
Source
npm
Version published
Weekly downloads
895K
2.53%
Maintainers
1
Weekly downloads
 
Created

What is json-schema-to-typescript?

The json-schema-to-typescript npm package is a tool that converts JSON Schema definitions into TypeScript interfaces. This is particularly useful for ensuring type safety in TypeScript projects by generating types directly from JSON Schema, which can help in maintaining consistency between your data models and TypeScript types.

What are json-schema-to-typescript's main functionalities?

Convert JSON Schema to TypeScript Interface

This feature allows you to convert a JSON Schema into a TypeScript interface. The `compile` function takes a JSON Schema and a name for the resulting TypeScript interface, and returns a promise that resolves to the TypeScript code as a string.

const { compile } = require('json-schema-to-typescript');

const schema = {
  title: 'Example Schema',
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number' }
  },
  required: ['name', 'age']
};

compile(schema, 'ExampleSchema')
  .then(ts => console.log(ts));

Generate TypeScript Definitions from Multiple Schemas

This feature allows you to generate TypeScript definitions from a JSON Schema file. The `compileFromFile` function reads a JSON Schema from a file and returns a promise that resolves to the TypeScript code as a string.

const { compileFromFile } = require('json-schema-to-typescript');

compileFromFile('path/to/schema.json')
  .then(ts => console.log(ts));

Customizing Output

This feature allows you to customize the output of the TypeScript definitions. The `compile` function accepts an options object where you can specify various settings like adding a banner comment to the generated TypeScript code.

const { compile } = require('json-schema-to-typescript');

const schema = {
  title: 'Example Schema',
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number' }
  },
  required: ['name', 'age']
};

const options = {
  bannerComment: '/* This is a generated file */'
};

compile(schema, 'ExampleSchema', options)
  .then(ts => console.log(ts));

Other packages similar to json-schema-to-typescript

Keywords

json

FAQs

Package last updated on 14 Jan 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