npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ephys/zod-to-ts

v2.3.2

Published

Generate TypeScript types from your Zod schema

Readme

zod-to-ts

generate TypeScript types from your Zod schema

Installation

npm install @ephys/zod-to-ts zod@^4 typescript

printZodAsTs

printZodAsTs is used to generate the TypeScript types from your Zod schemas, as a string.

import { z } from 'zod';
import { printZodAsTs } from '@ephys/zod-to-ts';

// define your Zod schema
const UserSchema = z.object({
  username: z.string(),
  age: z.number(),
});

// pass schema and name of type/identifier
const typings = printZodAsTs({ schemas: [UserSchema] });

result:

{
  username: string;
  age: number;
}

If you specify an identifier, it will be used as the type name:

import { z } from 'zod';
import { printZodAsTs } from '@ephys/zod-to-ts';

// define your Zod schema
const UserSchema = z
  .object({
    username: z.string(),
    age: z.number(),
  })
  .meta({ id: 'User' });

// pass schema and name of type/identifier
const typings = printZodAsTs({ schemas: [UserSchema] });

result:

type User = {
  username: string;
  age: number;
};

You can specify multiple schemas at once. In this case, you must specify the identifiers for each schema in the array:

import { z } from 'zod';
import { printZodAsTs } from '@ephys/zod-to-ts';

const UserSchema = z
  .object({
    username: z.string(),
    age: z.number(),
  })
  .meta({ id: 'User' });

const PostSchema = z
  .object({
    title: z.string(),
    content: z.string(),
    author: UserSchema,
  })
  .meta({ id: 'Post' });

const typings = printZodAsTs({
  schemas: [UserSchema, PostSchema],
});

result:

type User = {
  username: string;
  age: number;
};

type Post = {
  title: string;
  content: string;
  author: User;
};

Overriding Types

If you want to replace the generated TypeScript type with a custom one, you can use the overwriteTsOutput option.

import { z } from 'zod';
import { printZodAsTs } from '@ephys/zod-to-ts';

const DateSchema = z.instanceof(Date);

const UserSchema = z
  .object({
    username: z.string(),
    bornAt: DateSchema,
  })
  .meta({ id: 'User' });

const typings = printZodAsTs({
  schemas: [UserSchema],
  overwriteTsOutput(zodType, factory, modifiers) {
    if (schema === DateSchema) {
      return factory.createTypeReferenceNode('Date', undefined);
    }

    // if you do not return anything, the default behavior will be used
  },
});

Result:

type User = {
  username: string;
  bornAt: Date;
};

You can also return another schema, which will be the one converted to TypeScript:

import { z } from 'zod';
import { printZodAsTs } from '@ephys/zod-to-ts';

const Name = z.string();
const UpperCaseName = Name.transform((name) => name.toUpperCase());

const UserSchema = z
  .object({
    username: UpperCaseName,
  })
  .meta({ id: 'User' });

const typings = printZodAsTs({
  schemas: [UserSchema],
  overwriteTsOutput(zodType, factory, modifiers) {
    // transforms cannot be converted to TypeScript types directly,
    // so we can return the original schema to be converted instead
    if (schema === UpperCaseName) {
      return Name;
    }

    // if you do not return anything, the default schema will be used
  },
});

Result:

type User = {
  username: string;
};

Some zod types cannot be converted to TypeScript types directly, such as z.instanceof(Date).

TypeScript AST Viewer can help a lot with this if you are having trouble referencing something. It even provides copy-pastable code!

Circular References

If you have circular references in your Zod schemas, you must break the loop by adding the schemas that are part of the cycle to the schemas array and name them.

This won't work. It will throw an error because "UserSchema" will not be deduplicated:

import { z } from 'zod';
import { printZodAsTs } from '@ephys/zod-to-ts';

const UserSchema = z.object({
  username: z.string(),
  friends: z.array(z.lazy(() => UserSchema)), // circular reference
});

const FamilySchema = z.object({
  familyName: z.string(),
  members: z.array(UserSchema),
});

const typings = printZodAsTs({
  schemas: [FamilySchema],
});

But this will work:

import { z } from 'zod';
import { printZodAsTs } from '@ephys/zod-to-ts';

const UserSchema = z
  .object({
    username: z.string(),
    friends: z.array(z.lazy(() => UserSchema)), // circular reference
  })
  .meta({ id: 'User' });

const FamilySchema = z
  .object({
    familyName: z.string(),
    members: z.array(UserSchema),
  })
  .meta({ id: 'Family' });

const typings = printZodAsTs({
  schemas: [FamilySchema, UserSchema],
});

result:

type User = {
  username: string;
  friends: User[]; // circular reference
};

type Family = {
  familyName: string;
  members: User[];
};

convertZodToTs and printNode

If you want to convert Zod schemas to TypeScript AST nodes instead of strings, you can use convertZodToTs. You can then use printNode to convert the AST nodes to strings.