Compile JSON Schema to TypeScript typings.
Note: This is a fork of
json-schema-to-typescript
whose intention is to support the usedependencies
within JSON schemas. Right now the fork supports them when used in combination withoneOf
and may not actually support other use-cases currently.
Check out the live demo.
Input:
{
"title": "Example Schema",
"type": "object",
"properties": {
"occupation": {
"enum": ["farmer", "other"]
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
},
"hairColor": {
"enum": ["black", "brown", "blue"],
"type": "string"
}
},
"dependencies": {
"occupation": {
"oneOf": [
{
"properties": {
"occupation": {
"enum": ["farmer"]
}
},
{
"required": ["jobDescription"],
"properties": {
"occupation": {
"enum": ["other"]
},
"jobDescription": {
"type": "string"
}
}
}
}
]
}
},
"additionalProperties": false,
"required": ["firstName", "lastName", "occupation"]
}
Output:
export type ExampleSchema = ExampleSchema1 & {
firstName: string;
lastName: string;
occupation: "farmer" | "other"
/**
* Age in years
*/
age?: number;
hairColor?: "black" | "brown" | "blue";
}
export type ExampleSchema1 =
| {
occupation?: "farmer"
}
| {
occupation?: "other",
jobDescription: string
}
npm install json-schema-to-typescript-with-deps
json-schema-to-typescript is easy to use via the CLI, or programmatically.
First make the CLI available using one of the following options:
# install locally, then use `npx json2ts`
npm install json-schema-to-typescript-with-deps
# or install globally, then use `json2ts`
npm install json-schema-to-typescript-with-deps --global
# or install to npm cache, then use `npx --package=json-schema-to-typescript-with-deps json2ts`
# (you don't need to run an install command first)
Then, use the CLI to convert JSON files to TypeScript typings:
cat foo.json | json2ts > foo.d.ts
# or
json2ts foo.json > foo.d.ts
# or
json2ts foo.yaml foo.d.ts
# or
json2ts --input foo.json --output foo.d.ts
# or
json2ts -i foo.json -o foo.d.ts
# or (quote globs so that your shell doesn't expand them)
json2ts -i 'schemas/**/*.json'
# or
json2ts -i schemas/ -o types/
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.
# generate code for definitions that aren't referenced
json2ts -i foo.json -o foo.d.ts --unreachableDefinitions
# use single quotes and disable trailing semicolons
json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi
See the original repo for more usage and API examples.