Socket
Book a DemoInstallSign in
Socket

json-literal-typer

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-literal-typer

generates literal typescript iterfaces from JSON

0.1.1
latest
Source
npmnpm
Version published
Weekly downloads
41
46.43%
Maintainers
1
Weekly downloads
 
Created
Source

JSON literal typer

npm version unit-tests npm-publish codecov

There are some tools out there that will produce TypeScript Interfaces from a given JSON structure for you. But they only give you the basic types (string, number, etc.). What if there is an API where you want to get its literal values/types?

Install

# to add to your project
npm install json-literal-typer
# to use command line interface
npm install --global json-literal-typer

Demo

There is a live demo at https://json-literal-typer.peppnet.de

Example

Let's assume there is an API for gas stations. It will respond with data like this:

{
  "stations": [
    { "id": 1, "name": "station A", "status": "OPEN", "attributes": ["fast"] },
    {
      "id": 2,
      "name": "station B",
      "status": "OPEN",
      "attributes": ["fast", "24/7"],
      "operator": "Station Corp."
    },
    {
      "id": 3,
      "name": "station C",
      "status": "CLOSED",
      "attributes": [],
      "operator": "ACME Inc."
    }
  ]
}

What you would get from other tools is:

interface Station {
  id: number;
  name: string;
  status: string;
  attributes: string[];
  operator?: string;
}
interface RootObject {
  stations: Station[];
}

Nice. But what if you could get:

interface Stations {
  attributes: ('24/7' | 'fast')[];
  id: 1 | 2 | 3;
  name: 'station A' | 'station B' | 'station C';
  operator?: 'ACME Inc.' | 'Station Corp.';
  status: 'CLOSED' | 'OPEN';
}

interface Root {
  stations: Stations[];
}

CLI

npm show json-literal-typer versions --json | json-literal-typer
  • json-to-ts - Convert jsons to typescript interfaces
  • json2ts - generate TypeScript interfaces from JSON
  • quicktype - Generate types and converters from JSON, Schema, and GraphQL
  • ts-ast-viewer - TypeScript AST viewer

Keywords

json

FAQs

Package last updated on 08 Nov 2020

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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.