@ephys/zod-to-ts

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

从您的 Zod 方案生成类型脚本

中文使用文档
文档目录5 节
  1. 安装
  2. printZodAsTs
  3. 覆盖类型
  4. 循环参考文献
  5. convertZodToTs 财务报告和审定财务报表 printNode

半径

从您生成类型脚本 佐德语Name 计划

安装

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

printZodAsTs

printZodAsTs 用于从您的 Zod 计划生成 TypeScript 类型,作为字符串.

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] });

结果 :

{
  username: string;
  age: number;
}

若为 您指定一个标识符,它将作为类型名称:

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] });

结果 :

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

您可以同时指定多个方案 。 在这种情况下,您必须指定阵列中每个图案的标识符:

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],
});

结果 :

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

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

覆盖类型

如果您想要用自定义类型替换生成的类型Script, 您可以使用 overwriteTsOutput 选项.

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
  },
});

结果 :

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

您也可以返回另一个方案,即转换为 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
  },
});

结果 :

type User = {
  username: string;
};

一些zod类型不能直接转换为类型Script类型,例如 z.instanceof(Date)。 。 。 .

脚本 AST 查看器 如果你不能参考一些东西的话 这点可以帮助很多 它甚至提供了可复制的代码!

循环参考文献

如果您的 Zod 计划中有循环引用, 您必须突破循环, 将周期中包含的策略添加到 schemas 阵列并命名它们.

这行不通。 它会抛出一个错误, 因为“ UserSchema” 将不会被复制 :

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],
});

但是,这将奏效:

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],
});

结果 :

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

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

convertZodToTs 财务报告和审定财务报表 printNode

如果您想要将 Zod 计划转换为 TypeScript AST 节点而不是字符串, 您可以使用 convertZodToTs。 。 。 . 然后可以使用 printNode 将 AST 节点转换为字符串.

版本与发布记录

@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 是什么?

从您的 Zod 方案生成类型脚本

如何安装 @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 和提交记录判断。