{"id":13485219,"url":"https://github.com/bcherny/json-schema-to-typescript","last_synced_at":"2025-05-14T09:02:58.310Z","repository":{"id":39907568,"uuid":"54443780","full_name":"bcherny/json-schema-to-typescript","owner":"bcherny","description":"Compile JSON Schema to TypeScript type declarations","archived":false,"fork":false,"pushed_at":"2025-01-14T05:17:22.000Z","size":45457,"stargazers_count":3060,"open_issues_count":166,"forks_count":408,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-05-07T08:00:00.531Z","etag":null,"topics":["json-schema","typescript"],"latest_commit_sha":null,"homepage":"https://bcherny.github.io/json-schema-to-typescript-browser/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bcherny.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-22T03:56:58.000Z","updated_at":"2025-05-07T07:23:35.000Z","dependencies_parsed_at":"2024-04-23T11:02:48.265Z","dependency_job_id":"4095f80b-445c-49dd-aa34-bb1839d67ca8","html_url":"https://github.com/bcherny/json-schema-to-typescript","commit_stats":{"total_commits":532,"total_committers":66,"mean_commits":8.06060606060606,"dds":0.424812030075188,"last_synced_commit":"25c1d2752ff4bad135d6fc53e80c2f99784b0e6f"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcherny%2Fjson-schema-to-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcherny%2Fjson-schema-to-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcherny%2Fjson-schema-to-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcherny%2Fjson-schema-to-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcherny","download_url":"https://codeload.github.com/bcherny/json-schema-to-typescript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254110373,"owners_count":22016391,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-schema","typescript"],"created_at":"2024-07-31T17:01:51.598Z","updated_at":"2025-05-14T09:02:58.244Z","avatar_url":"https://github.com/bcherny.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","typescript","TypeScript 工具/库/框架"],"sub_categories":["构建工具"],"readme":"# json-schema-to-typescript [![Build Status][build]](https://github.com/bcherny/json-schema-to-typescript/actions?query=branch%3Amaster+workflow%3ACI) [![npm]](https://www.npmjs.com/package/json-schema-to-typescript) [![mit]](https://opensource.org/licenses/MIT) ![node]\n\n[build]: https://img.shields.io/github/actions/workflow/status/bcherny/json-schema-to-typescript/ci.yml?style=flat-square\n[npm]: https://img.shields.io/npm/v/json-schema-to-typescript.svg?style=flat-square\n[mit]: https://img.shields.io/npm/l/json-schema-to-typescript.svg?style=flat-square\n[node]: https://img.shields.io/badge/Node.js-16+-417e37?style=flat-square\n\n\u003e Compile JSON Schema to TypeScript typings.\n\n## Example\n\nCheck out the [live demo](https://borischerny.com/json-schema-to-typescript-browser/).\n\nInput:\n\n```json\n{\n  \"title\": \"Example Schema\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"firstName\": {\n      \"type\": \"string\"\n    },\n    \"lastName\": {\n      \"type\": \"string\"\n    },\n    \"age\": {\n      \"description\": \"Age in years\",\n      \"type\": \"integer\",\n      \"minimum\": 0\n    },\n    \"hairColor\": {\n      \"enum\": [\"black\", \"brown\", \"blue\"],\n      \"type\": \"string\"\n    }\n  },\n  \"additionalProperties\": false,\n  \"required\": [\"firstName\", \"lastName\"]\n}\n```\n\nOutput:\n\n```ts\nexport interface ExampleSchema {\n  firstName: string;\n  lastName: string;\n  /**\n   * Age in years\n   */\n  age?: number;\n  hairColor?: \"black\" | \"brown\" | \"blue\";\n}\n```\n\n## Installation\n\n```sh\nnpm install json-schema-to-typescript\n```\n\n## Usage\n\njson-schema-to-typescript is easy to use via the CLI, or programmatically.\n\n### CLI\n\nFirst make the CLI available using one of the following options:\n\n```sh\n# install locally, then use `npx json2ts`\nnpm install json-schema-to-typescript\n\n# or install globally, then use `json2ts`\nnpm install json-schema-to-typescript --global\n\n# or install to npm cache, then use `npx --package=json-schema-to-typescript json2ts`\n# (you don't need to run an install command first)\n```\n\nThen, use the CLI to convert JSON files to TypeScript typings:\n\n```sh\ncat foo.json | json2ts \u003e foo.d.ts\n# or\njson2ts foo.json \u003e foo.d.ts\n# or\njson2ts foo.yaml foo.d.ts\n# or\njson2ts --input foo.json --output foo.d.ts\n# or\njson2ts -i foo.json -o foo.d.ts\n# or (quote globs so that your shell doesn't expand them)\njson2ts -i 'schemas/**/*.json'\n# or\njson2ts -i schemas/ -o types/\n```\n\nYou 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.\n\n```sh\n# generate code for definitions that aren't referenced\njson2ts -i foo.json -o foo.d.ts --unreachableDefinitions\n# use single quotes and disable trailing semicolons\njson2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi\n```\n\n### API\n\nTo invoke json-schema-to-typescript from your TypeScript or JavaScript program, import it and call `compile` or `compileFromFile`.\n\n```js\nimport { compile, compileFromFile } from 'json-schema-to-typescript'\n\n// compile from file\ncompileFromFile('foo.json')\n  .then(ts =\u003e fs.writeFileSync('foo.d.ts', ts))\n\n// or, compile a JS object\nlet mySchema = {\n  properties: [...]\n}\ncompile(mySchema, 'MySchema')\n  .then(ts =\u003e ...)\n```\n\nSee [server demo](example) and [browser demo](https://github.com/bcherny/json-schema-to-typescript-browser) for full examples.\n\n## Options\n\n`compileFromFile` and `compile` accept options as their last argument (all keys are optional):\n\n| key | type | default | description |\n|-|-|-|-|\n| additionalProperties | boolean | `true` | Default value for `additionalProperties`, when it is not explicitly set |\n| 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 |\n| customName | `(LinkedJSONSchema, string \\| undefined) =\u003e string \\| undefined` | `undefined` | Custom function to provide a type name for a given schema\n| cwd | string | `process.cwd()` | Root directory for resolving [`$ref`](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html)s |\n| declareExternallyReferenced | boolean | `true` | Declare external schemas referenced via `$ref`? |\n| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |\n| inferStringEnumKeysFromValues | boolean | `false` | Create enums from JSON enums with eponymous keys |\n| format | boolean | `true` | Format code? Set this to `false` to improve performance. |\n| ignoreMinAndMaxItems | boolean | `false` | Ignore maxItems and minItems for `array` types, preventing tuples being generated. |\n| 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`.\n| strictIndexSignatures | boolean | `false` | Append all index signatures with `\\| undefined` so that they are strictly typed. |\n| style | object | `{ bracketSpacing: false,  printWidth: 120,  semi: true,  singleQuote: false,  tabWidth: 2,  trailingComma: 'none',  useTabs: false }` | A [Prettier](https://prettier.io/docs/en/options.html) configuration |\n| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |\n| unreachableDefinitions | boolean | `false` | Generates code for `$defs` that aren't referenced by the schema. |\n| $refOptions | object | `{}` | [$RefParser](https://github.com/APIDevTools/json-schema-ref-parser) Options, used when resolving `$ref`s |\n\n## Tests\n\n```sh\n$ npm test\n```\n\n## Features\n\n- [x] `title` =\u003e `interface`\n- [x] Primitive types:\n  - [x] array\n  - [x] homogeneous array\n  - [x] boolean\n  - [x] integer\n  - [x] number\n  - [x] null\n  - [x] object\n  - [x] string\n  - [x] homogeneous enum\n  - [x] heterogeneous enum\n- [x] Non/extensible interfaces\n- [ ] Custom JSON-schema extensions\n- [x] Nested properties\n- [x] Schema definitions\n- [x] [Schema references](http://json-schema.org/latest/json-schema-core.html#rfc.section.7.2.2)\n- [x] Local (filesystem) schema references\n- [x] External (network) schema references\n- [x] Add support for running in browser\n- [x] default interface name\n- [x] infer unnamed interface name from filename\n- [x] `deprecated`\n- [x] `allOf` (\"intersection\")\n- [x] `anyOf` (\"union\")\n- [x] `oneOf` (treated like `anyOf`)\n- [x] `maxItems` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L166))\n- [x] `minItems` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L165))\n- [x] `additionalProperties` of type\n- [x] `patternProperties` (partial support)\n- [x] [`extends`](https://github.com/json-schema/json-schema/wiki/Extends/014e3cd8692250baad70c361dd81f6119ad0f696)\n- [x] `required` properties on objects ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L130))\n- [ ] `validateRequired` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L124))\n- [x] literal objects in enum ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L236))\n- [x] referencing schema by id ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L331))\n- [x] custom typescript types via `tsType`\n\n## Custom schema properties:\n\n- `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](https://github.com/sokra/json-schema-to-typescript/blob/f1f40307cf5efa328522bb1c9ae0b0d9e5f367aa/test/e2e/customType.ts)).\n- `tsEnumNames`: Overrides the names used for the elements in an enum. Can also be used to create string enums ([eg](https://github.com/johnbillion/wp-json-schemas/blob/647440573e4a675f15880c95fcca513fdf7a2077/schemas/properties/post-status-name.json)).\n\n## Not expressible in TypeScript:\n\n- `dependencies` ([single](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L261), [multiple](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L282))\n- `divisibleBy` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L185))\n- [`format`](https://github.com/json-schema/json-schema/wiki/Format) ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L209))\n- `multipleOf` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L186))\n- `maximum` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L183))\n- `minimum` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L182))\n- `maxProperties` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L113))\n- `minProperties` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L112))\n- `not`/`disallow`\n- `oneOf` (\"xor\", use `anyOf` instead)\n- `pattern` ([string](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L203), [regex](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L207))\n- `uniqueItems` ([eg](https://github.com/tdegrunt/jsonschema/blob/67c0e27ce9542efde0bf43dc1b2a95dd87df43c3/examples/all.js#L172))\n\n## FAQ\n\n### JSON-Schema-to-TypeScript is crashing on my giant file. What can I do?\n\nPrettier is known to run slowly on really big files. To skip formatting and improve performance, set the `format` option to `false`.\n\n## Further Reading\n\n- JSON-schema spec: https://tools.ietf.org/html/draft-zyp-json-schema-04\n- JSON-schema wiki: https://github.com/json-schema/json-schema/wiki\n- JSON-schema test suite: https://github.com/json-schema/JSON-Schema-Test-Suite/blob/node\n- TypeScript spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md\n\n## Who uses JSON-Schema-to-TypeScript?\n\n- [Alibaba](https://github.com/alibaba/lowcode-engine)\n- [Amazon](https://github.com/aws/aws-toolkit-vscode), [AWSLabs](https://github.com/awslabs/cdk8s)\n- [Expo](https://github.com/expo/expo)\n- [FormatJS](https://github.com/formatjs/formatjs)\n- [Microsoft](https://github.com/microsoft/mixed-reality-extension-sdk)\n- [Mozilla](https://github.com/mdn/browser-compat-data)\n- [Nx](https://github.com/nrwl/nx)\n- [RStudio](https://github.com/rstudio/rstudio)\n- [Sourcegraph](https://github.com/sourcegraph/sourcegraph)\n- [Stryker](https://github.com/stryker-mutator/stryker)\n- [Webpack](https://github.com/webpack/webpack)\n- [See more](https://github.com/bcherny/json-schema-to-typescript/network/dependents?package_id=UGFja2FnZS0xNjUxOTM5Mg%3D%3D)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcherny%2Fjson-schema-to-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcherny%2Fjson-schema-to-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcherny%2Fjson-schema-to-typescript/lists"}