47ng / nuqs

Type-safe search params state manager for React frameworks - Like useState, but stored in the URL query string.
https://nuqs.47ng.com
MIT License
4.73k stars 103 forks source link

`clearOnDefault` doesn't work with `createSerializer` #641

Closed hugotiger closed 1 month ago

hugotiger commented 1 month ago

Context

What's your version of nuqs?

"nuqs": "1.19.1"

Next.js information (obtained by running next info):

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:59 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6030
  Available memory (MB): 36864
  Available CPU cores: 11
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.18.0
  pnpm: N/A
Relevant Packages:
  next: 14.2.11 // There is a newer version (14.2.12) available, upgrade recommended! 
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.5.4
Next.js Config:
  output: N/A
 ⚠ There is a newer version (14.2.12) available, upgrade recommended! 
   Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
   Read more - https://nextjs.org/docs/messages/opening-an-issue

Are you using:

Description

The clearOnDefault option does not seem to work when using createSerializer. Seeing as TS was happy I expected it to work. I love this feature from the hooks so would be amazing getting it to work!

Let me know if I should attempt adding/fixing this and I'll take a stab at it :)

Reproduction

I skipped creating a CodeSandbox playground since it's very easy to explain. Let me know if I still should create one 😄

const serialize = createSerializer({
    int: parseAsInteger.withOptions({ clearOnDefault: true }).withDefault(0)
})
const result = serialize({ int: 0 })
//    ^ Result is `?int=0`
franky47 commented 1 month ago

Ah yes, that would make sense for the serializer to follow this setting. If you want to open a PR, that'd be great, thanks!

hugotiger commented 1 month ago

Ah yes, that would make sense for the serializer to follow this setting. If you want to open a PR, that'd be great, thanks!

Awesome! I've opened https://github.com/47ng/nuqs/pull/642 where I seem to have fixed it, but haven't figured out how to type it properly yet so it has some @ts-expect-error directives. Can continue looking into it when I have some more time!

github-actions[bot] commented 1 month ago

:tada: This issue has been resolved in version 1.19.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: