Registry / devops / tozod
library3.0.0jsnpmunverified

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 tozod
INSTALL
IMPORT
SIG · TOZOD
T
tozod
devopsjavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

toZod
import { toZod } from 'tozod';
import toZod from 'tozod';
toZod is a named export, not a default export.
toZod (type usage)
type Schema = toZod<MyType>;
const s: toZod<MyType> = ...
Use toZod as a type annotation in variable declarations, not as a runtime value.
z.late.object
z.late.object(() => ({ ... }));
z.late.object({ ... });
z.late.object expects a factory function for recursive schemas; it is currently undocumented but exported by Zod.

Defines a Zod schema that is statically checked to match a TypeScript type, ensuring correctness at compile time.

import { z } from 'zod'; import { toZod } from 'tozod'; type Player = { name: string; age?: number | undefined; active: boolean | null; }; // Without toZod, if you forget .nullable() on active, no error. // With toZod, the type ensures the schema matches Player exactly. const Player: toZod<Player> = z.object({ name: z.string(), age: z.number().optional(), active: z.boolean().nullable(), }); console.log(Player.parse({ name: 'Alice', active: null })); // { name: 'Alice', active: null }
Debug
Known issues
breakingtoZod v3.0.0 requires TypeScript 3.9+ and strictNullChecks: true. Without strictNullChecks, the type inference may fail.
fix
Enable strictNullChecks in tsconfig.json: "strictNullChecks": true.
affects: >=3.0.0
gotchaz.late.object is undocumented but required for recursive types. Using z.object directly with a recursive type will cause a circular reference error at compile time.
fix
Use z.late.object(() => ({ ... fields referencing the type ... })) for recursive or mutually recursive schemas.
affects: >=3.0.0
deprecatedz.late.object may be replaced by z.object.lazy in future Zod versions. Check Zod changelog.
fix
Monitor Zod releases; adapt to z.object.lazy if it becomes stable.
affects: >=3.0.0
gotchatoZod validates the structure of the schema, not the runtime values. It does not enforce refinements like .uuid() or .min() to match the type; it only checks the shape.
fix
Add refinements manually as needed; toZod only ensures that the schema's shape (object keys, optional/nullable modifiers) matches the type.
affects: >=3.0.0
Errors
Common errors & fixes
Type 'ZodObject<...>' is not assignable to type 'toZod<Player>'.
The schema structure does not exactly match the type, e.g., missing .nullable() or .optional() on a field.
fix
Ensure every field uses the correct Zod method: .optional() for optional properties, .nullable() for null, etc.
Circular reference in type inference
Using z.object directly in a recursive type definition without z.late.object.
fix
Replace z.object({ ... }) with z.late.object(() => ({ ... })) for recursive schemas.
Cannot import toZod: Module not found
ToZod is not installed or is imported incorrectly.
fix
Run 'npm install tozod' or 'yarn add tozod', and use correct import: import { toZod } from 'tozod'.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
zodrequiredToZod is a utility for Zod; it requires Zod for schema definition.
Agent activity
8 hits · last 30 days
node
8
Resources
packagetozod
tozod — npm install tozod · libregistry