半径
从您生成类型脚本 佐德语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 节点转换为字符串.