Skip to content

Repository files navigation

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?

Featurejson-ts (this library)json-to-tsjson2ts
simple literal types (number, string etc)YESYESYES
array type, all elements of same kindYESYESYES
merge multiple json filesYES (cli, v1.6 & above)NONO
optional membersYESYESNO
array union typesYESYESNO
correct handling of top-level values (strings, arrays, arrays of objects, numbers etc)YESNONO
recursive data structures (see here)YESNONO
nested type literals (to account for invalid interface names)YESYESNO
output @flow typesYESNONO
WebsiteYESYESYES
CLIYESNONO
APIYESYESNO

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:

interfaceIRootObject{userId: number;id: number;title: string;body: string;}

Usage (API)

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

... produces the following:

interfaceIRootObject{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

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

Core

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

CLI

  • CLI tool to accept stdin (with --stdin flag)
  • 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)

About

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

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages