@ephys/zod-to-ts

中文文档
2.4.0发布于 2026/05/05TypeScriptES Module近 30 天下载 6.4万

Generate TypeScript types from your Zod schema

原始使用文档
文档目录5 节
  1. Installation
  2. printZodAsTs
  3. Overriding Types
  4. Circular References
  5. convertZodToTs and printNode

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.

版本与发布记录

@ephys/zod-to-ts 共发布 7 个版本,首次发布于 2025/07/25;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.4.0latest2026/05/056 个月前
2.3.22025/07/271.3 年前
2.3.12025/07/271.3 年前
2.3.02025/07/271.3 年前
2.2.02025/07/271.3 年前
2.1.02025/07/271.3 年前

关于 @ephys/zod-to-ts 的常见问题

@ephys/zod-to-ts 是什么?

Generate TypeScript types from your Zod schema

如何安装 @ephys/zod-to-ts?

在项目目录运行 npm install @ephys/zod-to-ts,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^4.9.4 || ^5.0.2 || ^6.0.0)、zod(^4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ephys/zod-to-ts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ephys/zod-to-ts 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ephys/zod-to-ts 可以商用吗?

@ephys/zod-to-ts 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@ephys/zod-to-ts 的下载量有多少?

根据 npm 官方下载统计,@ephys/zod-to-ts 在 2026/10/14 – 2026/11/12 这 30 天内被下载 64,499 次,日均约 2,150 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ephys/zod-to-ts 还在维护吗?

最新版本 2.4.0 发布于 2026/05/05(6 个月前);至今共发布 7 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/07/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。