TheEdoRan / next-safe-action

Type safe and validated Server Actions in your Next.js project.
https://next-safe-action.dev
MIT License
2.06k stars 32 forks source link

[BUG] Not compatible with Typescript >= 5.5.2 #180

Closed Talent30 closed 1 month ago

Talent30 commented 3 months ago

Are you using the latest version of this library?

Is there an existing issue for this?

Describe the bug

parsedInput returning any, schema defined by Zod

Will provide a minimal repo soon.

Reproduction steps

  1. Install Typescript 5.5.2
  2. Create a safe action with Zod schema, check the type of parsedInput

Expected behavior

parsedInput should have the correct type information

Minimal reproduction example

Code Sandbox link

Operating System

MacOS 14

Library version

7.2.0

Next.js version

14.2.4

Additional context

No response

TheEdoRan commented 3 months ago

Will look into it soon, thanks.

pedro757 commented 3 months ago

I can confirm schema validations don't work with typescript 5.5.2, I get any from parsedInput

TheEdoRan commented 2 months ago

After some tests, I identified the problem in TypeSchema, which breaks with TypeScript 5.5. I've just opened an issue on the TypeSchema repo for this bug:

TheEdoRan commented 2 months ago

Might be related to:

RolandRFuchs92 commented 2 months ago

I know this is a pretty new issue, but a quick easy workaround for me was to just downgrade typescript. I just locked at typescript 5.4.5 (I use the LSP that comes with Mason) for now, it seems to work perfectly.

TheEdoRan commented 2 months ago

@RolandRFuchs92 yes, the workaround is to downgrade to TypeScript v5.4. The issue appears to be related to a compiler change in TS 5.5.2, which broke ts-patch and so TypeSchema as well.

TheEdoRan commented 2 months ago

I've just released next-safe-action v7.1.4-experimental.1, that should work fine with TypeScript 5.5. Note that this version only works with Zod, it's essentially the same codebase without TypeSchema support, as it's currently preventing the ability to upgrade to TS 5.5.

So, if you're using Zod and TS 5.5, you can install experimental next-safe-action with this command:

npm i next-safe-action@experimental # or pnpm, yarn

P.S.: Even though the tag is named "experimental", as explained above the codebase is essentially the same as the stable v7. It's named like that because I don't think this will be an actual library change, as it's nice to have multiple validation libraries support. However, it's also nice to use the latest TS version, so until TypeSchema is compatible with it, I think this is a good solution.

damjtoh commented 2 months ago

I've just released next-safe-action v7.1.4-experimental.1, that should work fine with TypeScript 5.5. Note that this version only works with Zod, it's essentially the same codebase without TypeSchema support, as it's currently preventing the ability to upgrade to TS 5.5.

So, if you're using Zod and TS 5.5, you can install experimental next-safe-action with this command:

npm i next-safe-action@experimental # or pnpm, yarn

P.S.: Even though the tag is named "experimental", as explained above the codebase is essentially the same as the stable v7. It's named like that because I don't think this will be an actual library change, as it's nice to have multiple validation libraries support. However, it's also nice to use the latest TS version, so until TypeSchema is compatible with it, I think this is a good solution.

Hey! Just to let you know is working for me using TS 5.5 and zod. Thanks for your amazing work!

mattiaz9 commented 2 months ago

I'm trying the experimental version, but I can't destruct the response because is possibly undefined.

TheEdoRan commented 2 months ago

@mattiaz9 that's not an experimental version bug, it's the expected behavior since v7.0.0. The explanation for that can be found here.

mattiaz9 commented 2 months ago

@mattiaz9 that's not an experimental version bug, it's the expected behavior since v7.0.0. The explanation for that can be found here.

I see. I was previously using a pre-release of v7 and it wasn't yet the case, so I thought it was an error.

It didn't help the fact that in the docs that behavior is not shown. For instance in this example res is destructed without checking if undefined: https://next-safe-action.dev/docs/getting-started#3-import-and-execute-the-action

TheEdoRan commented 2 months ago

@mattiaz9

It didn't help the fact that in the docs that behavior is not shown.

Fixed. It was left over from v6 docs.

aniketbiprojit commented 2 months ago

This should probably be added to docs as well if not already there.

TheEdoRan commented 2 months ago

@aniketbiprojit https://next-safe-action.dev/docs/troubleshooting#schema-and-parsedinput-are-typed-any-broken-types-and-build-issues

But I'll add a note on the getting started page too, soon.

Also, I'm thinking of implementing a built-in system to support a small number of validation libraries (zod, valibot, yup), instead of relying on a third party library.

TheEdoRan commented 2 months ago

This issue should be solved in v7.2.0. next-safe-action now ships with a built-in system for supporting multiple validation libraries, at this time Zod (the default one), Valibot and Yup. You can now use them even with the Edge Runtime and obviously with TS >= 5.5. More information about that in this discussion and on this page of the website.

Please let me know if you encounter any bugs when updating to v7.2.0, thanks!

EDIT: Be sure to update to v7.2.1, which solves a build bug introduced in v7.2.0.

benjick commented 1 month ago

Any chance getting this fixed in v6 too?