Skip to content

[BUG] useAction.inputissue with zod-form-dataΒ #322

@armandabric

Description

@armandabric

Are you using the latest version of this library?

  • I verified that the issue exists in the latest next-safe-action release

Is there an existing issue for this?

  • I have searched the existing issues and found nothing that matches

Describe the bug

When using next-safe-action with zod-safe-action schema, the useAction(...).input type is not inferred correctly.

In the action body the input type is ok:

const schema = zfd.formData({
  bar: zfd.text(z.string().min(3).max(10)),
});

export const doSomethingWithZodFormData = actionClient
  .schema(schema)
  .action(async ({ parsedInput: { bar } }) => {
    // Here the `bar` input is a `string` as expected
    const baz: string = bar;

    return { /* ... */ };
  });

The issue is only with the hook:

const doSomethingWithZodFormActionAction = useAction(doSomethingWithZodFormData)

// With a schema made with zod-safe-action, the input type is not ok: `FormData | FormDataLikeInput`
const bar = doSomethingWithZodFormActionAction.input?.bar

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 two useAction usage: one with a zod schema, the other with a zod-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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions