如何修改Zod输出类型?以Flavor类型UserId为例
问题:让Zod Schema输出Flavor类型而非原始类型
我们通过Flavor类型定义了UserId:
interface Flavoring<FlavorT> { _type?: FlavorT; } type Flavor<T, FlavorT> = T & Flavoring<FlavorT>; type UserId = Flavor<string, 'UserId'>;
我们希望Zod Schema推断出的类型是UserId,而非普通的string。目前使用refine方法可以实现这一效果,但想找到无需额外函数调用的更优方案:
const MySchema = z.object({ id: z.string().refine((v): v is UserId => true), }); type MySchemaType = z.infer<typeof MySchema>; // 推断结果: // type MySchemaType = { // id: string & Flavoring<"UserId">; // }
内容的提问来源于stack exchange,提问作者TmTron
相关产品推荐
相关产品推荐

