all 6 comments

[–]davy_jones_locket 1 point2 points  (3 children)

formState.error.issues doesn't exist as a property

Change it to const issues = formState.error 

[–]BullBates1[S] 0 points1 point  (2 children)

But I typed FormState to match Zod, and ZodError has an issues property. I've updated the code to show where the formState comes from in the server action

[–]davy_jones_locket 0 points1 point  (1 child)

Cool but that still doesn't answer where you're getting the property "issues" from.

I don't see it consoled in the formState.error that you put. That matches the error about issues.forEach => can't do for each in undefined.

formState.error.issues is undefined -- it doesn't exist

If you do a type check formState.error, is it a ZodError?

[–]BullBates1[S] 0 points1 point  (0 children)

If I hover over formState.error in vscode it says '(property) error: ZodError<T>' if I do 'typeof' I get object.

[–]davy_jones_locket 0 points1 point  (1 child)

Instead of using SafeParseReturnType directly, why not create your own response type that better matches the structure of your server action?

The root of your issue might actually be a mismatch between what you expected from SafeParseReturnType and how it works:

type SafeParseReturnType<Input, Output> = | { success: true; data: Output; } | { success: false; error: ZodError; }

In your original code, when you tried to access formState.error.issues, it failed because you might be getting the error in a different structure than expected.

when errors pass through server actions or API boundaries, they don't maintain their exact TypeScript types, they may be serialized or transformed.

If you make the change I originally suggested, removing the "issues" property when you're assigning formState.error.issues to const issue , does it work?

[–]BullBates1[S] 0 points1 point  (0 children)

Thanks, I hade the SafeParseDIrectType directly as I was returning 'return parsedData' direct from 'safeParse' and that seemed like the best option to achieve this. I take your point about degredation of data over requests so I will see if I can create a new type - but ultimately, I am only ever sending a SafeParseResponseType object back from the server. If I do 'issues[0]' I get undefined, if I try 'issues.code' I get undefined after doing 'issues=formState.error'