Package Exports
- json-ts
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
json-ts
Automatically generate Typescript Definition files based on JSON input. This allows a frontend to verify it's using a backend correctly & optionally tie it into a CI environment.
Install
npm install -g json-ts
Usage (CLI)
Note: only stdin is supported right now, which requires the --stdin flag. Later I will add support for Windows, reading data from disk/network requests
curl https://jsonplaceholder.typicode.com/posts/1 | json-ts --stdin
... 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
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
- 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
- union types for array that contain mixed types:
nums: [1, "2"] -> nums: number|string[]
- handle none-valid names - where it's valid in JSON (cuz it's a string) - but not a valid identifier in output TS file
- Allow wrapping in namespace: eg:
declare namespace Projects { export interface ILoc { lat: number; lng: number; } ... }
CLI
- 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)