P粉2259617492023-08-31 17:26:21
您可以使用 discriminatedUnion
來實現此目的:
const productProperties = z.object({ name: z.string().nonempty(), description: z.string().nonempty(), price: z.number().positive(), category: z.string().nonempty(), brand: z.string().nonempty(), isFeatured: z.boolean().default(false), }) const product = z.discriminatedUnion('isLimitedEdition', [ productProperties.extend({ isLimitedEdition: z.literal(false) }), productProperties.partial().extend({ isLimitedEdition: z.literal(true) }), ])