Automatically generate Typescript Definition files or Flow types from JSON input
npm install json-ts
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
|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 |
bash
install
npm install -g json-tsrun against a single JSON file
json-ts dir/myfile.jsonrun 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.`bash
piping via stdin
curl https://jsonplaceholder.typicode.com/posts/1 | json-ts --stdinreading single json file from disk
json-ts my-file.jsonreading multiple json files from disk
json-ts my-file.json my-other-file.json
`... produces the following:
`ts
interface IRootObject {
userId: number;
id: number;
title: string;
body: string;
}
`Usage (API)
`bash
npm install json-ts --save-dev
``js
const { json2ts } = require('json-ts');
const json = ;
console.log(json2ts(json))
`... produces the following:
`ts
interface IRootObject {
name: string;
}
`For more examples, see the Tests
Options
- namespace: string - if provided, interfaces will be wrapped in a namespace (see below)
`bash
# usage
json-ts --namespace
# example
json-ts data/my-file.json --namespace API
`
- flow: boolean - output types in Flow format.
`bash
# usage
json-ts --flow
# example
json-ts data/my-file.json --flow
`
- prefix: string - override the I prefix on interface names
`bash
# usage
json-ts --prefix
# example (remove prefix)
json-ts data/my-file.json --prefix ""
`
- rootName: string - override the RootObject name of the top-level interface
`bash
# usage
json-ts --rootName
# example
json-ts data/my-file.json --rootName "Product"
`TODO:
$3
- [x] Allow choice of
I prefix on interface names
- [x] Allow naming of RootObject
- [ ] Allow choice of spaces/tabs$3
- [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:
`ts
declare namespace Projects {
export interface ILoc {
lat: number;
lng: number;
}
...
}
`
$3
- [x] CLI tool to accept stdin (with --stdin` flag)