From 0c46ca04d3fba8d7e676895df793902e7d5b1cf8 Mon Sep 17 00:00:00 2001 From: Mendes Hugo <37412330+HugoMendes98@users.noreply.github.com> Date: Wed, 18 Sep 2024 20:23:34 +0000 Subject: [PATCH 1/2] test: add failing tests --- src/__tests__/error.test.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/__tests__/error.test.ts b/src/__tests__/error.test.ts index b1942743b..746b2904d 100644 --- a/src/__tests__/error.test.ts +++ b/src/__tests__/error.test.ts @@ -309,9 +309,16 @@ test("formatting", () => { if (!result2.success) { type FormattedError = z.inferFormattedError; const error: FormattedError = result2.error.format(() => 5); - expect(error._errors).toEqual([]); - expect(error.inner?._errors).toEqual([]); - expect(error.inner?.name?._errors).toEqual([5]); + + type FormattedErrors = number[] | undefined; + const schemaErrors: FormattedErrors = error._errors; + const innerErrors: FormattedErrors = error.inner?._errors; + // @ts-expect-error it should not be the default formatted type + const nameErrors: string[] | undefined = error.inner?.name?._errors; + + expect(schemaErrors).toEqual([]); + expect(innerErrors).toEqual([]); + expect(nameErrors).toEqual([5]); } }); From bd7312f860446b0a52df92491f86182d05ad016c Mon Sep 17 00:00:00 2001 From: Mendes Hugo <37412330+HugoMendes98@users.noreply.github.com> Date: Wed, 18 Sep 2024 20:27:03 +0000 Subject: [PATCH 2/2] fix: add the formatted type in `ZodFormattedError` type recursion --- src/ZodError.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ZodError.ts b/src/ZodError.ts index c1f7aa3ee..db36ccacd 100644 --- a/src/ZodError.ts +++ b/src/ZodError.ts @@ -177,17 +177,17 @@ export const quotelessJson = (obj: any) => { return json.replace(/"([^"]+)":/g, "$1:"); }; -type recursiveZodFormattedError = T extends [any, ...any[]] - ? { [K in keyof T]?: ZodFormattedError } +type recursiveZodFormattedError = T extends [any, ...any[]] + ? { [K in keyof T]?: ZodFormattedError } : T extends any[] - ? { [k: number]: ZodFormattedError } + ? { [k: number]: ZodFormattedError } : T extends object - ? { [K in keyof T]?: ZodFormattedError } + ? { [K in keyof T]?: ZodFormattedError } : unknown; export type ZodFormattedError = { _errors: U[]; -} & recursiveZodFormattedError>; +} & recursiveZodFormattedError, U>; export type inferFormattedError< T extends ZodType,