TypeScript 转 Zod Schema

在线将 TypeScript interface 和 type 声明转换为 Zod 验证 schema,支持联合类型、可选属性、数组、枚举、嵌套对象及类型交叉引用。免费,纯浏览器端运行。

100% 浏览器端运行 数据不离开你的设备 免费 · 无需注册
TypeScript 输入
Zod Schema 输出

使用方法

  1. 将 TypeScript interfacetype 声明粘贴到左侧输入框。
  2. 工具会实时验证并转换(停止输入 300 毫秒后自动触发)。
  3. 点击 生成 Zod Schema 强制执行转换。
  4. 点击 复制 将 Zod 输出复制到剪贴板。
  5. 将结果粘贴到项目中——输出已包含 import 语句和 export type 类型推断行。

支持的 TypeScript 特性

  • 基础类型stringnumberbooleannullundefinedanyunknownvoidneverbigintsymbol
  • 可选属性prop?: type.optional()
  • 联合类型A | Bz.union([A, B])
  • 字符串枚举“a” | “b” | “c”z.enum([“a”, “b”, “c”])
  • 数组T[] / Array<T>z.array(T)
  • 嵌套对象:内联对象类型 → z.object({})
  • 交叉引用:命名类型间相互引用 → Schema 常量
  • readonly:接受 readonly 修饰符,转换时忽略(Zod schema 默认不可变)
  • 工具类型Partial<T>Record<K, V>Promise<T>
  • 交叉类型A & Bz.intersection(A, B)
  • 元组[A, B]z.tuple([A, B])
  • 字面量类型“value”42truez.literal()

示例

给定以下 TypeScript 输入:

interface Address {
street: string;
city: string;
zipCode?: string;
}

export interface User {
id: number;
name: string;
role: "admin" | "user" | "guest";
age?: number;
isActive: boolean;
tags: string[];
address: Address;
}

export type Status = "active" | "inactive" | "pending";

工具生成:

import { z } from "zod";

export const AddressSchema = z.object({
street: z.string(),
city: z.string(),
zipCode: z.string().optional(),
});
export type Address = z.infer<typeof AddressSchema>;

export const UserSchema = z.object({
id: z.number(),
name: z.string(),
role: z.enum(["admin", "user", "guest"]),
age: z.number().optional(),
isActive: z.boolean(),
tags: z.array(z.string()),
address: AddressSchema,
});
export type User = z.infer<typeof UserSchema>;

export const StatusSchema = z.enum(["active", "inactive", "pending"]);
export type Status = z.infer<typeof StatusSchema>;

FAQ

支持哪些 TypeScript 语法结构?

支持 interface 和 type alias 声明,包括基础类型(string、number、boolean、null、undefined、any、unknown)、可选属性(?)、联合类型(A | B)、字符串枚举(字面量联合)、数组(T[] 和 Array<T>)、嵌套内联对象、声明类型间的交叉引用,以及 Partial<T>、Record<K, V>、Array<T>、Promise<T> 等泛型工具类型。

类型间的交叉引用如何处理?

如果输入中声明了多个 interface 或 type alias,对某个已声明类型名称的引用会被替换为对应的 Schema 常量。例如,字段类型为 Address 的会在输出中变为 AddressSchema。输入中未声明的类型则变为 z.unknown()(附带注释)。

生成的 Schema 兼容哪个版本的 Zod?

生成的 schema 与 Zod v3 和 Zod v4 均兼容。输出使用标准 API:z.object()、z.string()、z.union()、z.enum()、z.array()、z.optional()、z.literal()、z.record() 和 z.infer<>。

字符串联合类型如何处理?

字符串字面量联合(如 "admin" | "user" | "guest")会自动转换为 z.enum(["admin", "user", "guest"]) 以保持简洁。混合联合类型(如 string | null)则使用 z.union([z.string(), z.null()])。

可以转换 TypeScript class 或泛型类型吗?

class 和泛型类型定义超出当前支持范围。工具专注于 interface 和 type alias 的转换。应用于具体类型的泛型工具类型(Array<string>、Record<string, number>)是支持的。

代码会被发送到服务器吗?

不会。TypeScript 解析器和 Zod 代码生成器完全在浏览器中运行,源代码不会离开你的设备。