{"id":14957214,"url":"https://github.com/vercel/json-schema-to-ts","last_synced_at":"2025-09-30T03:30:45.013Z","repository":{"id":65976713,"uuid":"317238293","full_name":"vercel/json-schema-to-ts","owner":"vercel","description":"Infer typescript types from JSON schemas!","archived":false,"fork":true,"pushed_at":"2020-11-30T13:55:30.000Z","size":608,"stargazers_count":13,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-27T18:01:12.441Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"ThomasAribart/json-schema-to-ts","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vercel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-30T13:50:49.000Z","updated_at":"2024-09-17T03:35:28.000Z","dependencies_parsed_at":"2023-02-19T19:15:35.008Z","dependency_job_id":null,"html_url":"https://github.com/vercel/json-schema-to-ts","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vercel%2Fjson-schema-to-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vercel%2Fjson-schema-to-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vercel%2Fjson-schema-to-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vercel%2Fjson-schema-to-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vercel","download_url":"https://codeload.github.com/vercel/json-schema-to-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234695618,"owners_count":18873010,"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":[],"created_at":"2024-09-24T13:14:22.743Z","updated_at":"2025-09-30T03:30:44.656Z","avatar_url":"https://github.com/vercel.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"assets/header-round-medium.png\" width=\"100%\" align=\"center\" /\u003e\n\n\u003cp align=\"right\"\u003e\n  \u003ci\u003eIf you use this repo, star it ✨\u003c/i\u003e\n\u003c/p\u003e\n\n# Stop typing twice 🙅‍♂️\n\nA lot of projects use JSON schemas for runtime data validation along with TypeScript for static type checking.\n\nTheir code may look like this:\n\n```typescript\nconst dogSchema = {\n  type: \"object\",\n  properties: {\n    name: { type: \"string\" },\n    age: { type: \"integer\" },\n    hobbies: { type: \"array\", items: { type: \"string\" } },\n    favoriteFood: { enum: [\"pizza\", \"taco\", \"fries\"] },\n  },\n  required: [\"name\", \"age\"],\n};\n\ntype Dog = {\n  name: string;\n  age: number;\n  hobbies?: string[];\n  favoriteFood?: \"pizza\" | \"taco\" | \"fries\";\n};\n```\n\nBoth objects carry similar if not exactly the same information. This is a code duplication that can annoy developers and introduce bugs if not properly maintained.\n\nThat's when `json-schema-to-ts` comes to the rescue 💪\n\n## FromSchema\n\nThe `FromSchema` method lets you infer TS types directly from JSON schemas:\n\n```typescript\nimport { FromSchema } from \"json-schema-to-ts\";\n\nconst dogSchema = {\n  type: \"object\",\n  properties: {\n    name: { type: \"string\" },\n    age: { type: \"integer\" },\n    hobbies: { type: \"array\", items: { type: \"string\" } },\n    favoriteFood: { enum: [\"pizza\", \"taco\", \"fries\"] },\n  },\n  required: [\"name\", \"age\"],\n} as const;\n\ntype Dog = FromSchema\u003ctypeof dogSchema\u003e;\n// =\u003e Will infer the same type as above\n```\n\nSchemas can even be nested, as long as you don't forget the `as const` statement:\n\n```typescript\nconst catSchema = { ... } as const;\n\nconst petSchema = {\n  anyOf: [dogSchema, catSchema],\n} as const;\n\ntype Pet = FromSchema\u003ctypeof petSchema\u003e;\n// =\u003e Will work 🙌\n```\n\n\u003e The `as const` statement is used so that TypeScript takes the schema definition to the word (e.g. _true_ is interpreted as the _true_ constant and not widened as _boolean_). It is pure TypeScript and has zero impact on the compiled code.\n\n## Why use `json-schema-to-ts`?\n\nIf you're looking for runtime validation with added types, libraries like [yup](https://github.com/jquense/yup), [zod](https://github.com/vriad/zod) or [runtypes](https://github.com/pelotom/runtypes) may suit your needs while being easier to use!\n\nOn the other hand, JSON schemas have the benefit of being widely used, more versatile and reusable (swaggers, APIaaS...).\n\nIf you prefer to stick to them and can define your schemas in TS instead of JSON (importing JSONs `as const` is not available yet), then `json-schema-to-ts` is made for you:\n\n- ✅ **Schema validation** `FromSchema` raises TS errors on invalid schemas, based on [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/json-schema)'s definitions\n- ✨ **No impact on compiled code**: `json-schema-to-ts` only operates in type space. And after all, what's lighter than a dev-dependency?\n- 🍸 **DRYness**: Less code means less embarrassing typos\n- 🤝 **Consistency**: See that `string` that you used instead of an `enum`? Or this `additionalProperties` you confused with `additionalItems`? Or forgot entirely? Well, `json-schema-to-ts` does!\n- 🔧 **Reliability**: `FromSchema` is extensively tested against [AJV](https://github.com/ajv-validator/ajv), and covers all the use cases that can be handled by TS for now\\*\n- 🏋️‍♂️ **Help on complex schemas**: Get complex schemas right first time with instantaneous typing feedbacks! For instance, it's not obvious the following schema can never be validated:\n\n```typescript\nconst addressSchema = {\n  type: \"object\",\n  allOf: [\n    {\n      properties: {\n        street: { type: \"string\" },\n        city: { type: \"string\" },\n        state: { type: \"string\" },\n      },\n      required: [\"street\", \"city\", \"state\"],\n    },\n    {\n      properties: {\n        type: { enum: [\"residential\", \"business\"] },\n      },\n    },\n  ],\n  additionalProperties: false,\n} as const;\n```\n\nBut it is with `FromSchema`!\n\n```typescript\ntype Address = FromSchema\u003ctypeof addressSchema\u003e;\n// =\u003e never 🙌\n```\n\n\u003e \\*If `json-schema-to-ts` misses one of your use case, feel free to [open an issue](https://github.com/ThomasAribart/json-schema-to-ts/issues) 🤗\n\n## Table of content\n\n- [Installation](#installation)\n- [Use cases](#use-cases)\n  - [Const](#const)\n  - [Enums](#enums)\n  - [Primitive types](#primitive-types)\n  - [Arrays](#arrays)\n  - [Tuples](#tuples)\n  - [Objects](#objects)\n- [Combining schemas](#combining-schemas)\n  - [AnyOf](#anyof)\n  - [AllOf](#allof)\n  - [OneOf](#oneof)\n  - [Not and If-Then-Else](#not-and-if-then-else)\n\n## Installation\n\n```bash\n# npm\nnpm install --save-dev json-schema-to-ts\n\n# yarn\nyarn add --dev json-schema-to-ts\n```\n\n\u003e `json-schema-to-ts` requires TypeScript 3.3+. Activating `strictNullChecks` or using `strict` mode is recommended.\n\n## Use cases\n\n### Const\n\n```typescript\nconst fooSchema = {\n  const: \"foo\",\n} as const;\n\ntype Foo = FromSchema\u003ctypeof fooSchema\u003e;\n// =\u003e \"foo\"\n```\n\n### Enums\n\n```typescript\nconst enumSchema = {\n  enum: [true, 42, { foo: \"bar\" }],\n} as const;\n\ntype Enum = FromSchema\u003ctypeof enumSchema\u003e;\n// =\u003e true | 42 | { foo: \"bar\"}\n```\n\nYou can also go full circle with typescript `enums`.\n\n```typescript\nenum Food {\n  Pizza = \"pizza\",\n  Taco = \"taco\",\n  Fries = \"fries\",\n}\n\nconst enumSchema = {\n  enum: Object.values(Food),\n} as const;\n\ntype Enum = FromSchema\u003ctypeof enumSchema\u003e;\n// =\u003e Food\n```\n\n### Primitive types\n\n```typescript\nconst primitiveTypeSchema = {\n  type: \"null\", // \"boolean\", \"string\", \"integer\", \"number\"\n} as const;\n\ntype PrimitiveType = FromSchema\u003ctypeof primitiveTypeSchema\u003e;\n// =\u003e null, boolean, string or number\n```\n\n```typescript\nconst primitiveTypesSchema = {\n  type: [\"null\", \"string\"],\n} as const;\n\ntype PrimitiveTypes = FromSchema\u003ctypeof primitiveTypesSchema\u003e;\n// =\u003e null | string\n```\n\n\u003e For more complex types, refinment keywords like `required` or `additionalItems` will apply 🙌\n\n### Arrays\n\n```typescript\nconst arraySchema = {\n  type: \"array\",\n  items: { type: \"string\" },\n} as const;\n\ntype Array = FromSchema\u003ctypeof arraySchema\u003e;\n// =\u003e string[]\n```\n\n### Tuples\n\n```typescript\nconst tupleSchema = {\n  type: \"array\",\n  items: [{ type: \"boolean\" }, { type: \"string\" }],\n} as const;\n\ntype Tuple = FromSchema\u003ctypeof tupleSchema\u003e;\n// =\u003e [] | [boolean] | [boolean, string] | [boolean, string, ...unknown[]]\n```\n\n`FromSchema` supports the `additionalItems` keyword:\n\n```typescript\nconst tupleSchema = {\n  type: \"array\",\n  items: [{ type: \"boolean\" }, { type: \"string\" }],\n  additionalItems: false,\n} as const;\n\ntype Tuple = FromSchema\u003ctypeof tupleSchema\u003e;\n// =\u003e [] | [boolean] | [boolean, string]\n```\n\n```typescript\nconst tupleSchema = {\n  type: \"array\",\n  items: [{ type: \"boolean\" }, { type: \"string\" }],\n  additionalItems: { type: \"number\" },\n} as const;\n\ntype Tuple = FromSchema\u003ctypeof tupleSchema\u003e;\n// =\u003e [] | [boolean] | [boolean, string] | [boolean, string, ...number[]]\n```\n\n...as well as the `minItems` and `maxItems` keywords:\n\n```typescript\nconst tupleSchema = {\n  type: \"array\",\n  items: [{ type: \"boolean\" }, { type: \"string\" }],\n  minItems: 1,\n  maxItems: 2,\n} as const;\n\ntype Tuple = FromSchema\u003ctypeof tupleSchema\u003e;\n// =\u003e [boolean] | [boolean, string]\n```\n\n\u003e Additional items will only work if Typescript's `strictNullChecks` option is activated\n\n### Objects\n\n```typescript\nconst objectSchema = {\n  type: \"object\",\n  properties: {\n    foo: { type: \"string\" },\n    bar: { type: \"number\" },\n  },\n  required: [\"foo\"],\n} as const;\n\ntype Object = FromSchema\u003ctypeof objectSchema\u003e;\n// =\u003e { [x: string]: unknown; foo: string; bar?: number; }\n```\n\n`FromSchema` partially supports the `additionalProperties` and `patternProperties` keywords:\n\n- `additionalProperties` can be used to deny additional items.\n\n```typescript\nconst closedObjectSchema = {\n  ...objectSchema,\n  additionalProperties: false,\n} as const;\n\ntype Object = FromSchema\u003ctypeof closedObjectSchema\u003e;\n// =\u003e { foo: string; bar?: number; }\n```\n\n- Used on their own, `additionalProperties` and/or `patternProperties` can be used to type unnamed properties.\n\n```typescript\nconst openObjectSchema = {\n  type: \"object\",\n  additionalProperties: {\n    type: \"boolean\",\n  },\n  patternProperties: {\n    \"^S\": { type: \"string\" },\n    \"^I\": { type: \"integer\" },\n  },\n} as const;\n\ntype Object = FromSchema\u003ctypeof openObjectSchema\u003e;\n// =\u003e { [x: string]: string | number | boolean }\n```\n\n- However, when used in combination with the `properties` keyword, extra properties will always be typed as `unknown` to avoid conflicts.\n\n## Combining schemas\n\n### AnyOf\n\n```typescript\nconst anyOfSchema = {\n  anyOf: [\n    { type: \"string\" },\n    {\n      type: \"array\",\n      items: { type: \"string\" },\n    },\n  ],\n} as const;\n\ntype AnyOf = FromSchema\u003ctypeof anyOfSchema\u003e;\n// =\u003e string | string[]\n```\n\n`FromSchema` will correctly infer factored schemas:\n\n```typescript\nconst factoredSchema = {\n  type: \"object\",\n  properties: {\n    bool: { type: \"boolean\" },\n  },\n  required: [\"bool\"],\n  anyOf: [\n    {\n      properties: {\n        str: { type: \"string\" },\n      },\n      required: [\"str\"],\n    },\n    {\n      properties: {\n        num: { type: \"number\" },\n      },\n    },\n  ],\n} as const;\n\ntype Factored = FromSchema\u003ctypeof factoredSchema\u003e;\n// =\u003e {\n//  [x:string]: unknown;\n//  bool: boolean;\n//  str: string;\n// } | {\n//  [x:string]: unknown;\n//  bool: boolean;\n//  num?: number;\n// }\n```\n\n### OneOf\n\nBecause TypeScript misses [refinment types](https://en.wikipedia.org/wiki/Refinement_type), `FromSchema` will use the `oneOf` keyword in the same way as `anyOf`:\n\n```typescript\nconst catSchema = {\n  type: \"object\",\n  oneOf: [\n    {\n      properties: {\n        name: { type: \"string\" },\n      },\n      required: [\"name\"],\n    },\n    {\n      properties: {\n        color: { enum: [\"black\", \"brown\", \"white\"] },\n      },\n    },\n  ],\n} as const;\n\ntype Cat = FromSchema\u003ctypeof catSchema\u003e;\n// =\u003e {\n//  [x: string]: unknown;\n//  name: string;\n// } | {\n//  [x: string]: unknown;\n//  color?: \"black\" | \"brown\" | \"white\";\n// }\n\n// =\u003e FromSchema will not detect the following invalid obj 😱\nconst invalidCat: Cat = { name: \"Garfield\" };\n```\n\n### AllOf\n\n```typescript\nconst addressSchema = {\n  type: \"object\",\n  allOf: [\n    {\n      properties: {\n        address: { type: \"string\" },\n        city: { type: \"string\" },\n        state: { type: \"string\" },\n      },\n      required: [\"address\", \"city\", \"state\"],\n    },\n    {\n      properties: {\n        type: { enum: [\"residential\", \"business\"] },\n      },\n    },\n  ],\n} as const;\n\ntype Address = FromSchema\u003ctypeof addressSchema\u003e;\n// =\u003e {\n//   [x: string]: unknown;\n//   address: string;\n//   city: string;\n//   state: string;\n//   type?: \"residential\" | \"business\";\n// }\n```\n\n### Not and If-Then-Else\n\nFor the same reason as `oneOf` (missing refinment types), I feel like implementing the `not` and the `if/then/else` keywords in `FromSchema` would lead into a rabbit hole...\n\nBut I may be wrong! If you think that it can be implemented, feel free to [open an issue](https://github.com/ThomasAribart/json-schema-to-ts/issues) 🤗\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvercel%2Fjson-schema-to-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvercel%2Fjson-schema-to-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvercel%2Fjson-schema-to-ts/lists"}