{"id":12766,"library":"json-schema-to-ts","title":"JSON Schema to TypeScript Type Inferer","description":"json-schema-to-ts is a TypeScript library designed to infer static TypeScript types directly from existing JSON schemas. This eliminates the need for manual type duplication, mitigating potential bugs and reducing maintenance overhead in projects that rely on JSON schemas for runtime data validation. The current stable version is 3.1.1, with a release cadence that is quite active, typically seeing minor or patch updates on a monthly basis. A key differentiator from libraries like Zod, Yup, or Runtypes is its focus purely on type inference from the established JSON Schema standard, rather than providing an integrated validation and schema definition system. This makes it an ideal choice for development environments where JSON schemas are already a primary source of truth for API specifications (e.g., OpenAPI) or data contracts, enabling seamless integration of existing schemas for robust static type checking.","status":"active","version":"3.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/ThomasAribart/json-schema-to-ts","tags":["javascript","json","schema","typescript","type","ts"],"install":[{"cmd":"npm install json-schema-to-ts","lang":"bash","label":"npm"},{"cmd":"yarn add json-schema-to-ts","lang":"bash","label":"yarn"},{"cmd":"pnpm add json-schema-to-ts","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"FromSchema is a named export, primarily used as a type. Always use `typeof` with your schema object when passing it to FromSchema.","wrong":"import FromSchema from 'json-schema-to-ts'","symbol":"FromSchema","correct":"import { FromSchema } from 'json-schema-to-ts'"},{"note":"asConst is a runtime utility function to help TypeScript infer a 'const' type for schemas, an alternative to the `as const` assertion.","wrong":"const schema = asConst({...})","symbol":"asConst","correct":"import { asConst } from 'json-schema-to-ts'"},{"note":"JSONSchema is a type representing a valid JSON Schema object. Use `import type` as it's a type-only import, commonly used with the `satisfies` operator.","wrong":"import { JSONSchema } from 'json-schema-to-ts'","symbol":"JSONSchema","correct":"import type { JSONSchema } from 'json-schema-to-ts'"}],"quickstart":{"code":"import { FromSchema } from 'json-schema-to-ts';\n\nconst userSchema = {\n  type: 'object',\n  properties: {\n    id: { type: 'string', format: 'uuid' },\n    name: { type: 'string' },\n    email: { type: 'string', format: 'email' },\n    age: { type: 'integer', minimum: 0 },\n    isActive: { type: 'boolean', default: true },\n    roles: { type: 'array', items: { enum: ['admin', 'editor', 'viewer'] } },\n  },\n  required: ['id', 'name', 'email', 'age'],\n  additionalProperties: false,\n} as const;\n\ntype User = FromSchema<typeof userSchema>;\n\nconst newUser: User = {\n  id: 'a1b2c3d4-e5f6-7890-1234-567890abcdef',\n  name: 'Jane Doe',\n  email: 'jane.doe@example.com',\n  age: 30,\n  // isActive will be inferred as boolean and optional due to default, but is required by v3 behavior for defaulted properties.\n  roles: ['editor']\n};\n\nconsole.log(newUser);\n// Expected output: { id: '...', name: 'Jane Doe', email: 'jane.doe@example.com', age: 30, roles: [ 'editor' ] }","lang":"typescript","description":"This quickstart demonstrates how to define a JSON schema and infer its TypeScript type using `FromSchema`, along with how to instantiate an object conforming to that type."},"warnings":[{"fix":"Review schemas with `default` properties. If the property should remain optional in the TypeScript type, explicitly mark it as optional in the schema using `\"required\": [...]` and omitting the property name, or adjust your types where the property is used.","message":"In version 3.0.0, the type inference for properties with a `default` keyword changed. Previously, defaulted properties were inferred as optional (`?`). Since v3.0.0, they are inferred as required (`:`).","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always append `as const` to your schema definitions (e.g., `const mySchema = { ... } as const;`). Alternatively, use the `asConst` utility function provided by the library or the TypeScript 4.9+ `satisfies` operator.","message":"Forgetting the `as const` assertion on your JSON schema object will lead to TypeScript widening the types, resulting in less precise or incorrect inferred types (e.g., `\"string\"` becoming `string`, `\"true\"` becoming `boolean`).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade your TypeScript version to 4.9 or higher, or use the `as const` assertion or the `asConst` utility function instead.","message":"The `satisfies` operator, a modern alternative to `as const` for type-checking schemas, requires TypeScript version 4.9 or newer. Using it with older versions will result in a syntax error.","severity":"gotcha","affected_versions":"<4.9"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Add `as const` to your schema definition: `const mySchema = { ... } as const;`.","cause":"The schema object is not asserted as `const`, causing TypeScript to widen its literal types. `FromSchema` expects a 'const' type.","error":"Argument of type '{ type: string; properties: { name: { type: string; }; }; required: string[]; }' is not assignable to parameter of type 'JSONSchema'."},{"fix":"Ensure `as const` is applied directly to an object literal. If importing JSON, you might need a runtime assertion or convert it to a TypeScript literal type.","cause":"You are trying to apply `as const` to a non-literal expression, such as an imported JSON file that isn't cast as a literal, or a function return value.","error":"A 'const' assertion can only be applied to a string, number, boolean, array, or object literal."},{"fix":"Update your `typescript` dependency in `package.json` and your `tsconfig.json` settings to use TypeScript 4.9 or newer.","cause":"Your `tsconfig.json` targets a TypeScript version older than 4.9, which does not support the `satisfies` operator.","error":"The 'satisfies' operator is only available in TypeScript version 4.9 or newer."}],"ecosystem":"npm"}