From 4cf19606870e66bf4307984bf99a4bef495c7818 Mon Sep 17 00:00:00 2001 From: gydroperit Date: Tue, 28 Feb 2023 11:46:59 +0500 Subject: [PATCH] Refactoring of ZodFormattedError type to improve tsc check time (#2107) Co-authored-by: Roman Mezenin --- deno/lib/ZodError.ts | 16 +++++++++------- src/ZodError.ts | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/deno/lib/ZodError.ts b/deno/lib/ZodError.ts index def27ddfc..d3cb8e386 100644 --- a/deno/lib/ZodError.ts +++ b/deno/lib/ZodError.ts @@ -170,15 +170,17 @@ export const quotelessJson = (obj: any) => { return json.replace(/"([^"]+)":/g, "$1:"); }; +type recursiveZodFormattedError = T extends [any, ...any[]] + ? { [K in keyof T]?: ZodFormattedError } + : T extends any[] + ? { [k: number]: ZodFormattedError } + : T extends object + ? { [K in keyof T]?: ZodFormattedError } + : unknown; + export type ZodFormattedError = { _errors: U[]; -} & (NonNullable extends [any, ...any[]] - ? { [K in keyof NonNullable]?: ZodFormattedError[K], U> } - : NonNullable extends any[] - ? { [k: number]: ZodFormattedError[number], U> } - : NonNullable extends object - ? { [K in keyof NonNullable]?: ZodFormattedError[K], U> } - : unknown); +} & recursiveZodFormattedError>; export type inferFormattedError< T extends ZodType, diff --git a/src/ZodError.ts b/src/ZodError.ts index 0802c1793..9ad9a9e53 100644 --- a/src/ZodError.ts +++ b/src/ZodError.ts @@ -170,15 +170,17 @@ export const quotelessJson = (obj: any) => { return json.replace(/"([^"]+)":/g, "$1:"); }; +type recursiveZodFormattedError = T extends [any, ...any[]] + ? { [K in keyof T]?: ZodFormattedError } + : T extends any[] + ? { [k: number]: ZodFormattedError } + : T extends object + ? { [K in keyof T]?: ZodFormattedError } + : unknown; + export type ZodFormattedError = { _errors: U[]; -} & (NonNullable extends [any, ...any[]] - ? { [K in keyof NonNullable]?: ZodFormattedError[K], U> } - : NonNullable extends any[] - ? { [k: number]: ZodFormattedError[number], U> } - : NonNullable extends object - ? { [K in keyof NonNullable]?: ZodFormattedError[K], U> } - : unknown); +} & recursiveZodFormattedError>; export type inferFormattedError< T extends ZodType,