Gathering detailed insights and metrics for json-schema-to-typescript
Gathering detailed insights and metrics for json-schema-to-typescript
Gathering detailed insights and metrics for json-schema-to-typescript
Gathering detailed insights and metrics for json-schema-to-typescript
json-schema-to-ts
Infer typescript types from your JSON schemas!
json-schema-to-typescript-lite
Lite version of json-schema-to-typescript
json-schema-to-typescript-for-browser
compile json schema to typescript typings
fast-typescript-to-jsonschema
fast-typescript-to-jsonschema generates JSON Schema files from your Typescript sources.
Compile JSON Schema to TypeScript type declarations
npm install json-schema-to-typescript
Typescript
Module System
Min. Node Version
Node Version
NPM Version
92.9
Supply Chain
99.6
Quality
79.8
Maintenance
100
Vulnerability
98.9
License
TypeScript (99.87%)
JavaScript (0.13%)
Total Downloads
89,174,754
Last Day
34,902
Last Week
856,298
Last Month
3,691,802
Last Year
34,470,118
MIT License
3,098 Stars
593 Commits
409 Forks
18 Watchers
45 Branches
62 Contributors
Updated on Jul 05, 2025
Minified
Minified + Gzipped
Latest Version
15.0.4
Package Id
json-schema-to-typescript@15.0.4
Unpacked Size
196.43 kB
Size
47.32 kB
File Count
57
NPM Version
10.8.3
Node Version
20.14.0
Published on
Jan 14, 2025
Cumulative downloads
Total Downloads
Last Day
-5.7%
34,902
Compared to previous day
Last Week
-7.6%
856,298
Compared to previous week
Last Month
0.5%
3,691,802
Compared to previous month
Last Year
52.2%
34,470,118
Compared to previous year
9
Compile JSON Schema to TypeScript typings.
Check out the live demo.
Input:
1{ 2 "title": "Example Schema", 3 "type": "object", 4 "properties": { 5 "firstName": { 6 "type": "string" 7 }, 8 "lastName": { 9 "type": "string" 10 }, 11 "age": { 12 "description": "Age in years", 13 "type": "integer", 14 "minimum": 0 15 }, 16 "hairColor": { 17 "enum": ["black", "brown", "blue"], 18 "type": "string" 19 } 20 }, 21 "additionalProperties": false, 22 "required": ["firstName", "lastName"] 23}
Output:
1export interface ExampleSchema { 2 firstName: string; 3 lastName: string; 4 /** 5 * Age in years 6 */ 7 age?: number; 8 hairColor?: "black" | "brown" | "blue"; 9}
1npm install json-schema-to-typescript
json-schema-to-typescript is easy to use via the CLI, or programmatically.
First make the CLI available using one of the following options:
1# install locally, then use `npx json2ts` 2npm install json-schema-to-typescript 3 4# or install globally, then use `json2ts` 5npm install json-schema-to-typescript --global 6 7# or install to npm cache, then use `npx --package=json-schema-to-typescript json2ts` 8# (you don't need to run an install command first)
Then, use the CLI to convert JSON files to TypeScript typings:
1cat foo.json | json2ts > foo.d.ts 2# or 3json2ts foo.json > foo.d.ts 4# or 5json2ts foo.yaml foo.d.ts 6# or 7json2ts --input foo.json --output foo.d.ts 8# or 9json2ts -i foo.json -o foo.d.ts 10# or (quote globs so that your shell doesn't expand them) 11json2ts -i 'schemas/**/*.json' 12# or 13json2ts -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.
1# generate code for definitions that aren't referenced 2json2ts -i foo.json -o foo.d.ts --unreachableDefinitions 3# use single quotes and disable trailing semicolons 4json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi
To invoke json-schema-to-typescript from your TypeScript or JavaScript program, import it and call compile
or compileFromFile
.
1import { compile, compileFromFile } from 'json-schema-to-typescript' 2 3// compile from file 4compileFromFile('foo.json') 5 .then(ts => fs.writeFileSync('foo.d.ts', ts)) 6 7// or, compile a JS object 8let mySchema = { 9 properties: [...] 10} 11compile(mySchema, 'MySchema') 12 .then(ts => ...)
See server demo and browser demo for full examples.
compileFromFile
and compile
accept options as their last argument (all keys are optional):
key | type | default | description |
---|---|---|---|
additionalProperties | boolean | true | Default value for additionalProperties , when it is not explicitly set |
bannerComment | string | "/* eslint-disable */\n/**\n* This file was automatically generated by json-schema-to-typescript.\n* DO NOT MODIFY IT BY HAND. Instead, modify the source JSON Schema file,\n* and run json-schema-to-typescript to regenerate this file.\n*/" | Disclaimer comment prepended to the top of each generated file |
customName | (LinkedJSONSchema, string | undefined) => string | undefined | undefined | Custom function to provide a type name for a given schema |
cwd | string | process.cwd() | Root directory for resolving $ref s |
declareExternallyReferenced | boolean | true | Declare external schemas referenced via $ref ? |
enableConstEnums | boolean | true | Prepend enums with const ? |
inferStringEnumKeysFromValues | boolean | false | Create enums from JSON enums with eponymous keys |
format | boolean | true | Format code? Set this to false to improve performance. |
ignoreMinAndMaxItems | boolean | false | Ignore maxItems and minItems for array types, preventing tuples being generated. |
maxItems | number | 20 | Maximum number of unioned tuples to emit when representing bounded-size array types, before falling back to emitting unbounded arrays. Increase this to improve precision of emitted types, decrease it to improve performance, or set it to -1 to ignore maxItems . |
strictIndexSignatures | boolean | false | Append all index signatures with | undefined so that they are strictly typed. |
style | object | { bracketSpacing: false, printWidth: 120, semi: true, singleQuote: false, tabWidth: 2, trailingComma: 'none', useTabs: false } | A Prettier configuration |
unknownAny | boolean | true | Use unknown instead of any where possible |
unreachableDefinitions | boolean | false | Generates code for $defs that aren't referenced by the schema. |
$refOptions | object | {} | $RefParser Options, used when resolving $ref s |
1$ npm test
title
=> interface
deprecated
allOf
("intersection")anyOf
("union")oneOf
(treated like anyOf
)maxItems
(eg)minItems
(eg)additionalProperties
of typepatternProperties
(partial support)extends
required
properties on objects (eg)validateRequired
(eg)tsType
tsType
: Overrides the type that's generated from the schema. Useful for forcing a type to any
or when using non-standard JSON schema extensions (eg).tsEnumNames
: Overrides the names used for the elements in an enum. Can also be used to create string enums (eg).dependencies
(single, multiple)divisibleBy
(eg)format
(eg)multipleOf
(eg)maximum
(eg)minimum
(eg)maxProperties
(eg)minProperties
(eg)not
/disallow
oneOf
("xor", use anyOf
instead)pattern
(string, regex)uniqueItems
(eg)Prettier is known to run slowly on really big files. To skip formatting and improve performance, set the format
option to false
.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
1 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
20 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More