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 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-ts

v1.6.4

Published

Automatically generate Typescript Definition files or Flow types from JSON input

Downloads

16,254

Readme

Build Status

npm install -g json-ts

Automatically generate Typescript Definition files or Flow types from JSON input.

Use it via the API, CLI, or Website

json-ts

How does json-ts stack up against the competition?

|Feature |json-ts (this library) |json-to-ts |json2ts | |---|---|---|---| |simple literal types (number, string etc) |YES |YES |YES | |array type, all elements of same kind |YES |YES |YES | |merge multiple json files|YES (cli, v1.6 & above) |NO |NO | |optional members | YES | YES | NO | |array union types | YES |YES |NO | |correct handling of top-level values (strings, arrays, arrays of objects numbers etc) |YES |NO |NO | |recursive data structures (see here) |YES |NO |NO | |nested type literals (to account for invalid interface names) |YES |YES |NO | |output @flow types |YES |NO |NO | |Website |YES |YES |YES | |CLI |YES |NO |NO | |API |YES |YES |NO |

Quick-start

# install
npm install -g json-ts

# run against a single JSON file
json-ts dir/myfile.json

# run against multiple single JSON files (interfaces will be merged)
json-ts api/resp1.json api/resp2.json

Usage (CLI)

Note: only stdin (which requires the --stdin flag) & filepaths are supported right now. Later I will add support for Windows, reading data from network requests etc.

## piping via stdin
curl https://jsonplaceholder.typicode.com/posts/1 | json-ts --stdin

## reading single json file from disk
json-ts my-file.json

## reading multiple json files from disk
json-ts my-file.json my-other-file.json

... produces the following:

interface IRootObject {
  userId: number;
  id: number;
  title: string;
  body: string;
}

Usage (API)

npm install json-ts --save-dev
const { json2ts } = require('json-ts');
const json = `
{
    "name": "Shane"
}
`;
console.log(json2ts(json))

... produces the following:

interface IRootObject {
  name: string;
}

For more examples, see the Tests

Options

  • namespace: string - if provided, interfaces will be wrapped in a namespace (see below)
    # usage
    json-ts <filename> --namespace <namespace_name> 
       
    # example
    json-ts data/my-file.json --namespace API
  • flow: boolean - output types in Flow format.
    # usage
    json-ts <filename> --flow 
       
    # example
    json-ts data/my-file.json --flow
  • prefix: string - override the I prefix on interface names
    # usage
    json-ts <filename> --prefix <prefix_string> 
       
    # example (remove prefix)
    json-ts data/my-file.json --prefix ""
  • rootName: string - override the RootObject name of the top-level interface
    # usage
    json-ts <filename> --rootName <rootname_string> 
       
    # example
    json-ts data/my-file.json --rootName "Product"

TODO:

options

  • [x] Allow choice of I prefix on interface names
  • [x] Allow naming of RootObject
  • [ ] Allow choice of spaces/tabs

Core

  • [x] support array types
  • [x] support boolean types
  • [x] support null types
  • [x] output types for Flow via --flow
  • [x] PascalCase as default for all interface names
  • [x] de-dupe interfaces (it's dumb atm, POC)
  • [x] de-dupe interfaces where propname differs, but members are the same
  • [x] merge interfaces by creating union types for members
  • [x] union types for array that contain mixed literal types: nums: [1, "2"] -> nums: number|string[] (already works for complex objects)
  • [x] quoted member names when needed
  • [x] handle invalid name for interface
  • [x] support type alias declarations
  • [x] Use Typescript factory methods/printer for output
  • [x] Allow wrapping in namespace: eg:
        declare namespace Projects {
            export interface ILoc {
               lat: number;
               lng: number;
            }
            ...
        }

CLI

  • [x] CLI tool to accept stdin (with --stdin flag)
  • [x] CLI tool to accept json file as input
  • [ ] CLI tool to accept URL as input (for validating against remote API)
  • [ ] configurable output (filename/stdout etc)