Skip to content

TypeScript to Zod Schema Transformer — Numio

Generates Zod schemas from TypeScript interfaces and types with ts-to-zod. Each type Foo becomes an exported fooSchema, and JSDoc tags such as @format email or @minimum become Zod validators.

Your API types already exist as TypeScript interfaces and you want runtime validation of incoming requests without writing schemas by hand.

TypeScript

Zod Schema

When to use it

  • Your API types already exist as TypeScript interfaces and you want runtime validation of incoming requests without writing schemas by hand.
  • You validate form input with Zod and want the schema to stay consistent with the shared TypeScript type.

How to use

  1. Paste TypeScript interfaces or type aliases; add JSDoc tags like @minimum 13 or @format email to get validators.
  2. In settings, turn on Keep TSDoc Comments to copy comments into the output, or Skip the creation of zod validators from JSDoc annotations to ignore the tags.
  3. The source is converted on the server via /api/typescript-to-zod; copy the code that imports z from "zod".

TypeScript

export interface Signup {
  /**
   * @format email
   */
  email: string;
  /**
   * @minimum 13
   * @maximum 120
   */
  age: number;
  plan: "free" | "pro";
  referrer?: string | null;
  createdAt: Date;
}

Zod schemas

import { z } from "zod"

export const signupSchema = z.object({
  email: z.string().email(),
  age: z.number().min(13).max(120),
  plan: z.union([z.literal("free"), z.literal("pro")]),
  referrer: z.string().optional().nullable(),
  createdAt: z.date(),
})

Details and limitations

The page sends your input to a server route (/api/typescript-to-zod) for conversion; it is not processed only in the browser. Next.js API routes accept request bodies up to 1 MB by default.

Only types are converted. Exported functions and constants produce no schema.

If a type refers to a type that is not in the input, the converter reports a missing dependency error instead of emitting a schema.

Schemas are ordered by dependency, so a referenced type such as Author is emitted before the type that uses it.

FAQ

How are optional and null fields converted?

referrer?: string | null becomes z.string().optional().nullable(); a property typed Author | null becomes authorSchema.nullable().

How is Date handled?

A Date property becomes z.date(), which accepts Date objects, not ISO strings. Use z.coerce.date() for JSON input.

Why do I get "Some schemas can't be generated due to direct or indirect missing dependencies"?

A type references another type that is not in the pasted code. Paste the referenced types too.

Specifications and references