tozod v3.0.0 is a TypeScript utility for defining Zod schemas that are guaranteed to match a given TypeScript type. It inverts the typical Zod workflow: instead of inferring a type from a schema, tozod starts with a type and ensures the schema is structurally identical at compile time. It supports recursive and mutually recursive types via z.late.object, preserving full access to Zod's built-in methods like .omit(), .pick(), and .partial(). Requires TypeScript 3.9+ and strictNullChecks. Published as part of the Zod ecosystem.
npm install tozodNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Defines a Zod schema that is statically checked to match a TypeScript type, ensuring correctness at compile time.
Enable strictNullChecks in tsconfig.json: "strictNullChecks": true.
Use z.late.object(() => ({ ... fields referencing the type ... })) for recursive or mutually recursive schemas.Monitor Zod releases; adapt to z.object.lazy if it becomes stable.
Add refinements manually as needed; toZod only ensures that the schema's shape (object keys, optional/nullable modifiers) matches the type.
Ensure every field uses the correct Zod method: .optional() for optional properties, .nullable() for null, etc.
Replace z.object({ ... }) with z.late.object(() => ({ ... })) for recursive schemas.Run 'npm install tozod' or 'yarn add tozod', and use correct import: import { toZod } from 'tozod'.