colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.09k stars 1.15k forks source link

The mapped-type is not recursive in `ZodFormattedError` (`error.format()`) #3761

Open HugoMendes98 opened 2 days ago

HugoMendes98 commented 2 days ago

Hello, I've noticed that the mapped-type when formatting an error is not reused in the recursive type. This causes the “sub-levels” _errors of an object to be incorrectly typed.

Here's an example:

declare const error: z.ZodError<{ flat: number; object: { nested: number } }>;

const formatted = error.format(issue => !!issue.fatal);

const ok: boolean[] = formatted._errors ?? [];
// Type 'string[]' is not assignable to type 'boolean[]'
const ko1: boolean[] = formatted.flat?._errors ?? [];
// Type 'string[]' is not assignable to type 'boolean[]'
const ko2: boolean[] = formatted.object?.nested?._errors ?? [];

I solved the problem locally on my end and will probably submit a “PR” soon.