# @ephys/zod-to-ts 中文文档

规范地址：https://npmcn.com/package/%40ephys/zod-to-ts/

版本：2\.4\.0
npm 元数据更新时间：2026-05-05T07:48:47.424Z
中文译文更新时间：2026-11-13T19:30:33.680Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40ephys/zod-to-ts
原文对照：https://npmcn.com/package/%40ephys/zod-to-ts/?lang=original
项目主页：https://github.com/ephys/zod-to-ts#readme
源码仓库：https://github.com/ephys/zod-to-ts

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：40.5 kB，9 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：2 个
- peer 依赖：typescript ^4\.9\.4 \|\| ^5\.0\.2 \|\| ^6\.0\.0，zod ^4
- 首次发布：2025-07-25T15:34:59.920Z
- 最新版本发布：2026-05-05T07:48:47.334Z
- 近 30 天下载量：64499（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：7（近 12 个月 1 个）

## 中文简介

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

## 安装

```sh
npm install @ephys/zod-to-ts
```

也可以使用：`pnpm add @ephys/zod-to-ts`、`yarn add @ephys/zod-to-ts`、`bun add @ephys/zod-to-ts`

## README

# 半径

从您生成类型脚本 [佐德语Name](https://github.com/colinhacks/zod) 计划

## 安装

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

## `printZodAsTs`

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

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

结果 :

```ts
{
  username: string;
  age: number;
}
```

* * *

若为 [您指定一个标识符](https://zod.dev/metadata?id=meta),它将作为类型名称:

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

结果 :

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

* * *

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

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

结果 :

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

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

### 覆盖类型

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

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

结果 :

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

您也可以返回另一个方案,即转换为 TypeScript 的方案:

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

结果 :

```ts
type User = {
  username: string;
};
```

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

[脚本 AST 查看器](https://ts-ast-viewer.com/) 如果你不能参考一些东西的话 这点可以帮助很多 它甚至提供了可复制的代码!

### 循环参考文献

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

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

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

但是,这将奏效:

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

结果 :

```ts
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 是什么？

从您的 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 和提交记录判断。

