ts-to-zod is a powerful utility designed to automatically generate Zod schemas directly from existing TypeScript types and interfaces. Currently stable at version 5.1.0, the library maintains a relatively active release cadence, frequently incorporating new features and bug fixes, with major version bumps often synchronized with Zod's own major releases. Its core differentiation lies in its direct interaction with the TypeScript Abstract Syntax Tree (AST), ensuring high fidelity in schema generation. It also offers advanced features such as enhancing schemas via JSDoc tags (e.g., `@minimum`, `@maxLength`) and includes an internal validation step to verify that generated schemas are fully compatible with their original TypeScript types, minimizing discrepancies. This tool significantly streamlines the process of adding robust runtime validation to TypeScript projects.
npm install ts-to-zodVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to install ts-to-zod, generate Zod schemas from a TypeScript type definition file using the CLI, and then import and use the generated schemas for runtime validation.
Ensure your project is configured for ESM where ts-to-zod is imported, or verify your CJS setup correctly resolves module exports. Update Node.js to a supported version for optimal compatibility.
Update your project's Zod dependency to '^4' (e.g., `npm install zod@^4`) and regenerate all schemas. For Zod v3 support, use ts-to-zod v3.x series.
Integrate a code formatter like Prettier or BiomeJS into your build pipeline. The recommended approach is to use `pretty-quick` or a similar tool in your post-generation scripts.
Ensure all types you wish to be validated by ts-to-zod are exported in your TypeScript files. For private types, rely on your TypeScript compiler for type safety.
Avoid using `--skipValidation` unless absolutely necessary for debugging or specific edge cases. Address any validation errors reported by the tool to maintain schema integrity.
For Node.js projects, ensure your importing file is an ES Module (e.g., .mjs extension or 'type: module' in package.json) or adjust your build system to handle ESM. Alternatively, if your project needs to remain CommonJS, verify your Node.js version and module resolution settings are compatible with ts-to-zod's dual CJS/ESM exports.
First, ensure you are using ts-to-zod v4+ with Zod v4+. If the issue persists, check your original TypeScript type definition for correctness and verify if any JSDoc tags are causing unexpected schema generation. If possible, remove `--skipValidation` to let ts-to-zod's internal validation catch discrepancies during generation. If the problem seems to be a bug in ts-to-zod, report it on GitHub.
Update your project's Zod dependency to version 4 or higher (e.g., `npm install zod@^4`). If you must use Zod v3, use ts-to-zod v3.x series.