-
-
Notifications
You must be signed in to change notification settings - Fork 39
[BUG] useAction.input
issue with zod-form-data
#322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
TLDR; It's because I was once supporting this in my server action library chungweileong94/server-act#35, but I found myself fighting against the types for this specific use-case (zod-form-data), so I ended up ditching it when I adopted standard schema chungweileong94/server-act#37. |
This should be fixed in the latest |
@chungweileong94 thanks for fixing this issue in zod-form-data! |
I just try again with But it's hardly usable with
|
Ah, I see, but I don't think it's considered a bug in either if (!("entries" in selectDeliveryModeAction.input)) {
// The input is object here
console.log(selectDeliveryModeAction.input.mode);
} You can also use this helper function to check if the input is a form data type FormDataLikeInput = {
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
entries(): IterableIterator<[string, FormDataEntryValue]>;
};
function isFormDataLikeInput<T>(
input: FormData | FormDataLikeInput | T,
): input is FormData | FormDataLikeInput {
return "entries" in input;
}
...
if (!isFormDataLikeInput(selectDeliveryModeAction.input)) {
console.log(selectDeliveryModeAction.input.mode);
} |
I'm actually made a mistake on the helper function. This is the correct version. function isFormDataLikeInput<T extends Record<string, unknown>>(
input: FormData | FormDataLikeInput | T,
): input is FormData | FormDataLikeInput {
return "entries" in input;
} |
I agree. I'd say it impossible to use Edit: To add some context of that I'm trying to achive My expectation is to use the const { execute, input } = useAction(loginFormAction);
return (
<form className={styles.Form} action={execute}>
<input type="text" defaultValue={input.name} />
</form>
) I cant find any way to accomplish this at the moment. |
any updates regarding this issue? i also cannot use input |
Uh oh!
There was an error while loading. Please reload this page.
Are you using the latest version of this library?
Is there an existing issue for this?
Describe the bug
When using
next-safe-action
withzod-safe-action
schema, theuseAction(...).input
type is not inferred correctly.In the action body the input type is ok:
The issue is only with the hook:
The issue also present with the
useAction(...).execute
function.Everything works correctly when using a zod schema.
Reproduction steps
In the linked reproduction sandbox, if you open the
app/page.tsx
file you will see twouseAction
usage: one with azod
schema, the other with azod-safe-action
schema. The second case trigger a TS error.Expected behavior
As the input type is well inferred in the action, I'm expecting it to the be inferred when using the
useAction
hook.Link to a minimal reproduction of the issue
https://codesandbox.io/p/devbox/epic-torvalds-vt88n4
Library version
7.10.3
Next.js version
15.1.7
Node.js version
20.9.0
Additional context
No response
The text was updated successfully, but these errors were encountered: