const typeABaseSchema = z.strictObject({
type: z.literal("a"),
});
const typeASchema = typeABaseSchema.extend({
nested: z.lazy(() => typeSchema).optional(),
});
type TypeADef = z.infer<typeof typeABaseSchema> & {
nested?: TypeDef | undefined;
};
const typeBBaseSchema = z.strictObject({
type: z.literal("b"),
});
const typeBSchema = typeBBaseSchema.extend({
nested: z.lazy(() => typeSchema).optional(),
});
type TypeBDef = z.infer<typeof typeBBaseSchema> & {
nested?: TypeDef | undefined;
};
const typeA: z.ZodType<TypeADef> = typeASchema;
const typeB: z.ZodType<TypeBDef> = typeBSchema;
const typeSchema = z.union([typeA, typeB]);
type TypeDef = z.infer<typeof typeSchema>;
typeSchema.parse({ type: "a", nested: { type: "b" } });
typeSchema.parse({ type: "a", nested: { type: "a", nested: { type: "b" } } });
typeSchema.parse({ type: "b", nested: { type: "b", nested: { type: "a" } } });
const typeABaseSchema = z.strictObject({
type: z.literal("a"),
});
const typeASchema = typeABaseSchema.extend({
nested: z.lazy(() => typeSchema).optional(),
});
type TypeADef = z.infer<typeof typeABaseSchema> & {
nested?: TypeDef | undefined;
};
const typeBBaseSchema = z.strictObject({
type: z.literal("b"),
});
const typeBSchema = typeBBaseSchema.extend({
nested: z.lazy(() => typeSchema).optional(),
});
type TypeBDef = z.infer<typeof typeBBaseSchema> & {
nested?: TypeDef | undefined;
};
const typeA: z.ZodType<TypeADef> = typeASchema;
const typeB: z.ZodType<TypeBDef> = typeBSchema;
const typeSchema = z.union([typeA, typeB]);
type TypeDef = z.infer<typeof typeSchema>;
typeSchema.parse({ type: "a", nested: { type: "b" } });
typeSchema.parse({ type: "a", nested: { type: "a", nested: { type: "b" } } });
typeSchema.parse({ type: "b", nested: { type: "b", nested: { type: "a" } } });