zod-to-types
v0.1.1
Published
Generate TypeScript types from Zod schemas
Readme
zod-to-types
Generate TypeScript types from your Zod schemas.
Inspired by zod-to-ts
Installation
pnpm add zod-to-types zod typescriptUsage
import { z } from 'zod';
import { zodToTypes, printNode } from 'zod-to-types';
const UserSchema = z.object({
username: z.string(),
email: z.email(),
age: z.number(),
role: z.enum(['admin', 'user', 'guest']),
});
const { node } = zodToTypes(UserSchema);
console.log(printNode(node));
// Output: { username: string; email: string; age: number; role: "admin" | "user" | "guest"; }API
zodToTypes(schema, identifier?, options?)Options
{
enumStyle?: 'union' | 'resolve' // default: 'union'
}Function Types
z.function() in Zod v4+ creates a function factory ($ZodFunction), not a schema type. This is properly typed and supported:
const functionSchema = z.function({
input: [z.string()],
output: z.number(),
});
const { node } = zodToTypes(functionSchema); // Works correctlyLicense
MIT
