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.86k stars 104 forks source link

fix(serializer/types): Allow `null` value in serializer for parsers with default values #769

Closed MartinCura closed 6 days ago

MartinCura commented 6 days ago

See related discussion.

Passing null to the serialize of a search param whose parser has a default currently is a type error, even though the behaviour works as expected.

This is because for the values argument it infers the types using the same inferParserType which is fine for parsing but is not quite right for serializing. The way i'm fixing it is just declaring a different inferSerializerRecordType to be used here. If there's some TS-foo i don't know which allows reusing the same inferParserRecordType, that'd be even better!

CONTRIBUTING.md mentions "adding a minimal reproduction in the playground can be very helpful" but i couldn't immediately find how to create one there.

Added some very basic tests, feel free to point out if any more would be nice.

vercel[bot] commented 6 days ago

@MartinCura is attempting to deploy a commit to the 47ng Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] commented 6 days ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nuqs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 15, 2024 0:57am
pkg-pr-new[bot] commented 6 days ago

Open in Stackblitz

pnpm add https://pkg.pr.new/nuqs@769

commit: 877d1d3

franky47 commented 6 days ago

Feel free to test the pkg.pr.new build to see if it works on your side.

MartinCura commented 6 days ago

Feel free to test the pkg.pr.new build to see if it works on your side.

It does remove the type error ✅ image

I didn't see a changelog file so only thing i may be missing is being explicit about this in the docs, maybe, though not necessary.

Other than that, feel free to merge 🚀

franky47 commented 6 days ago

The changelog is generated by semantic-release in the GitHub release when the package gets published.

I think we can omit the docs, this is the expected behaviour. Thanks again for your contribution! 🙏

github-actions[bot] commented 6 days ago

:tada: This PR is included in version 2.2.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: