Skip to content

Commit

Permalink
Refactoring of ZodFormattedError type to improve tsc check time (#2107)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman Mezenin <roman.mezenin@rtds.team>
  • Loading branch information
gydroperit and Roman Mezenin authored Feb 28, 2023
1 parent d6f0890 commit 4cf1960
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
16 changes: 9 additions & 7 deletions deno/lib/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,17 @@ export const quotelessJson = (obj: any) => {
return json.replace(/"([^"]+)":/g, "$1:");
};

type recursiveZodFormattedError<T> = T extends [any, ...any[]]
? { [K in keyof T]?: ZodFormattedError<T[K]> }
: T extends any[]
? { [k: number]: ZodFormattedError<T[number]> }
: T extends object
? { [K in keyof T]?: ZodFormattedError<T[K]> }
: unknown;

export type ZodFormattedError<T, U = string> = {
_errors: U[];
} & (NonNullable<T> extends [any, ...any[]]
? { [K in keyof NonNullable<T>]?: ZodFormattedError<NonNullable<T>[K], U> }
: NonNullable<T> extends any[]
? { [k: number]: ZodFormattedError<NonNullable<T>[number], U> }
: NonNullable<T> extends object
? { [K in keyof NonNullable<T>]?: ZodFormattedError<NonNullable<T>[K], U> }
: unknown);
} & recursiveZodFormattedError<NonNullable<T>>;

export type inferFormattedError<
T extends ZodType<any, any, any>,
Expand Down
16 changes: 9 additions & 7 deletions src/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,17 @@ export const quotelessJson = (obj: any) => {
return json.replace(/"([^"]+)":/g, "$1:");
};

type recursiveZodFormattedError<T> = T extends [any, ...any[]]
? { [K in keyof T]?: ZodFormattedError<T[K]> }
: T extends any[]
? { [k: number]: ZodFormattedError<T[number]> }
: T extends object
? { [K in keyof T]?: ZodFormattedError<T[K]> }
: unknown;

export type ZodFormattedError<T, U = string> = {
_errors: U[];
} & (NonNullable<T> extends [any, ...any[]]
? { [K in keyof NonNullable<T>]?: ZodFormattedError<NonNullable<T>[K], U> }
: NonNullable<T> extends any[]
? { [k: number]: ZodFormattedError<NonNullable<T>[number], U> }
: NonNullable<T> extends object
? { [K in keyof NonNullable<T>]?: ZodFormattedError<NonNullable<T>[K], U> }
: unknown);
} & recursiveZodFormattedError<NonNullable<T>>;

export type inferFormattedError<
T extends ZodType<any, any, any>,
Expand Down

0 comments on commit 4cf1960

Please sign in to comment.