55[ mit ] : https://img.shields.io/npm/l/json-schema-to-typescript.svg?style=flat-square
66[ node ] : https://img.shields.io/badge/Node.js-16+-417e37?style=flat-square
77
8- > Compile json/yaml schema to typescript typings
8+ > Compile JSONSchema to TypeScript typings.
99
1010## Example
1111
12+ Check out the [ live demo] ( https://borischerny.com/json-schema-to-typescript-browser/ ) .
13+
1214Input:
15+
1316``` json
1417{
1518 "title" : " Example Schema" ,
3740```
3841
3942Output:
43+
4044``` ts
4145export interface ExampleSchema {
4246 firstName: string ;
@@ -52,15 +56,46 @@ export interface ExampleSchema {
5256## Installation
5357
5458``` sh
55- # Using Yarn:
56- yarn add json-schema-to-typescript
57-
58- # Or, using NPM:
59- npm install json-schema-to-typescript --save
59+ npm install json-schema-to-typescript
6060```
6161
6262## Usage
6363
64+ json-schema-to-typescript is easy to use via the CLI, or programmatically.
65+
66+ ### CLI
67+
68+ A CLI utility is provided with this package.
69+
70+ ``` sh
71+ cat foo.json | json2ts > foo.d.ts
72+ # or
73+ json2ts foo.json > foo.d.ts
74+ # or
75+ json2ts foo.yaml foo.d.ts
76+ # or
77+ json2ts --input foo.json --output foo.d.ts
78+ # or
79+ json2ts -i foo.json -o foo.d.ts
80+ # or (quote globs so that your shell doesn't expand them)
81+ json2ts -i ' schemas/**/*.json'
82+ # or
83+ json2ts -i schemas/ -o types/
84+ ```
85+
86+ You can pass any of the options described below (including style options) as CLI flags. Boolean values can be set to false using the ` no- ` prefix.
87+
88+ ``` sh
89+ # generate code for definitions that aren't referenced
90+ json2ts -i foo.json -o foo.d.ts --unreachableDefinitions
91+ # use single quotes and disable trailing semicolons
92+ json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi
93+ ```
94+
95+ ### API
96+
97+ To invoke json-schema-to-typescript from your TypeScript or JavaScript program, import it and call ` compile ` or ` compileFromFile ` .
98+
6499``` js
65100import { compile , compileFromFile } from ' json-schema-to-typescript'
66101
@@ -98,39 +133,13 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
98133| unknownAny | boolean | ` true ` | Use ` unknown ` instead of ` any ` where possible |
99134| unreachableDefinitions | boolean | ` false ` | Generates code for ` $defs ` that aren't referenced by the schema. |
100135| $refOptions | object | ` {} ` | [ $RefParser] ( https://github.com/APIDevTools/json-schema-ref-parser ) Options, used when resolving ` $ref ` s |
101- ## CLI
102136
103- A CLI utility is provided with this package.
104-
105- ``` sh
106- cat foo.json | json2ts > foo.d.ts
107- # or
108- json2ts foo.json > foo.d.ts
109- # or
110- json2ts foo.json foo.d.ts
111- # or
112- json2ts --input foo.json --output foo.d.ts
113- # or
114- json2ts -i foo.json -o foo.d.ts
115- # or (quote globs so that your shell doesn't expand them)
116- json2ts -i ' schemas/**/*.json'
117- # or
118- json2ts -i schemas/ -o types/
119- ```
120-
121- You can pass any of the options described above (including style options) as CLI flags. Boolean values can be set to false using the ` no- ` prefix.
137+ ## Tests
122138
123139``` sh
124- # generate code for definitions that aren't referenced
125- json2ts -i foo.json -o foo.d.ts --unreachableDefinitions
126- # use single quotes and disable trailing semicolons
127- json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi
140+ $ npm test
128141```
129142
130- ## Tests
131-
132- ` npm test `
133-
134143## Features
135144
136145- [x] ` title ` => ` interface `
0 commit comments