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.
npm install json-schema-to-tsVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.Upgrade your TypeScript version to 4.9 or higher, or use the `as const` assertion or the `asConst` utility function instead.
Add `as const` to your schema definition: `const mySchema = { ... } as const;`.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.
Update your `typescript` dependency in `package.json` and your `tsconfig.json` settings to use TypeScript 4.9 or newer.
No dependency data recorded yet.