zod-to-ts generates TypeScript type definitions directly from Zod schemas, converting Zod's runtime validation objects into static TypeScript types. The current stable version is `2.0.0`, with recent releases focusing on supporting Zod v4 and improving the handling of complex type structures, especially recursion. Releases appear to be feature-driven, with new minor versions adding capabilities and major versions introducing breaking changes, particularly around Zod compatibility and internal API improvements. Key differentiators include its ability to generate TypeScript AST nodes directly for programmatic manipulation, robust support for recursive types through an auxiliary type system, and configurable output (e.g., 'input' vs. 'output' types for schemas using transformations or pipes). It also supports JSDoc comments from Zod's `.describe()` method, making generated types more descriptive. This library is crucial for projects aiming to maintain type safety and reduce boilerplate by deriving types directly from Zod validation logic.
npm install zod-to-tsVerified import paths — ran on the pinned version, not inferred.
Demonstrates converting complex Zod object schemas, including nested objects, arrays, and recursive structures, into TypeScript type aliases and printing them, showing how auxiliary types are generated for recursion.
Ensure your project uses `zod@^4.0.0` or higher. Upgrade Zod if necessary using `npm install zod@^4`.
Update your type override configuration to use the new `Map`-based API as described in the documentation.
To bypass the error and generate `any` for unrepresentable types, pass `{ unrepresentable: 'any' }` in the options object to `zodToTs`. Alternatively, refactor your schema to avoid these constructs if strict type generation is required.Before calling `zodToTs` with a recursive schema, initialize `const auxiliaryTypeStore = createAuxiliaryTypeStore()` and pass it in the options: `{ auxiliaryTypeStore }`. Remember to process the `auxiliaryTypeStore.definitions` for all generated helper types.Upgrade your `zod` package to version 4 or higher: `npm install zod@^4`.
Pass `{ unrepresentable: 'any' }` in the options object to `zodToTs` to allow `any` to be generated for unrepresentable types, or modify your Zod schema to avoid these constructs.Use `Array.from(auxiliaryTypeStore.definitions.values())` to correctly convert the iterator into an array before further processing like `map()`.