Skip to content

Commit b8257d7

Browse files
committed
Improve tuple recursive inference.
1 parent dbb05ef commit b8257d7

File tree

3 files changed

+1039
-9
lines changed

3 files changed

+1039
-9
lines changed

‎packages/zod/src/v4/classic/tests/recursive-types.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,21 @@ test("object utilities with recursive types", () => {
366366
]);
367367
});
368368

369+
test("tuple with recursive types", () => {
370+
const TaskListNodeSchema = z.strictObject({
371+
type: z.literal("taskList"),
372+
get content() {
373+
return z.array(z.tuple([TaskListNodeSchema, z.union([TaskListNodeSchema])])).min(1);
374+
},
375+
});
376+
type TaskListNodeSchema = z.infer<typeof TaskListNodeSchema>;
377+
type _TaskListNodeSchema = {
378+
type: "taskList";
379+
content: [_TaskListNodeSchema, _TaskListNodeSchema][];
380+
};
381+
expectTypeOf<TaskListNodeSchema>().toEqualTypeOf<_TaskListNodeSchema>();
382+
});
383+
369384
test("recursion compatibility", () => {
370385
// array
371386
const A = z.object({

‎packages/zod/src/v4/core/schemas.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2282,9 +2282,12 @@ type TupleOutputTypeWithOptionals<T extends util.TupleItems> = T extends readonl
22822282
export interface $ZodTupleInternals<
22832283
T extends util.TupleItems = readonly $ZodType[],
22842284
Rest extends SomeType | null = $ZodType | null,
2285-
> extends $ZodTypeInternals<$InferTupleOutputType<T, Rest>, $InferTupleInputType<T, Rest>> {
2285+
> extends _$ZodTypeInternals {
22862286
def: $ZodTupleDef<T, Rest>;
22872287
isst: errors.$ZodIssueInvalidType | errors.$ZodIssueTooBig<unknown[]> | errors.$ZodIssueTooSmall<unknown[]>;
2288+
// $ZodTypeInternals<$InferTupleOutputType<T, Rest>, $InferTupleInputType<T, Rest>>
2289+
output: $InferTupleOutputType<T, Rest>;
2290+
input: $InferTupleInputType<T, Rest>;
22882291
}
22892292

22902293
export interface $ZodTuple<

0 commit comments

Comments
 (0)