Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/teal-cups-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@plainbrew/next-typed-href": patch
---

docs: document named factory pattern and reason for curried design in defineTypedHrefWithNuqs
8 changes: 6 additions & 2 deletions packages/next-typed-href/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ import type { AppRoutes, ParamsOf } from "@/../.next/types/routes";

type AppRouteParamsMap = { [Route in AppRoutes]: ParamsOf<Route> };

export const { $href } = defineTypedHrefWithNuqs<AppRoutes, AppRouteParamsMap>()({
const withNuqs = defineTypedHrefWithNuqs<AppRoutes, AppRouteParamsMap>();

export const { $href } = withNuqs({
"/search": {
q: parseAsString,
page: parseAsInteger,
Expand Down Expand Up @@ -116,7 +118,9 @@ $href({ route: "/users/[id]", routeParams: { id: "42" }, searchParams: { tab: "p
Parsers wrapped with `.withDefault()` make the type non-nullable and omit the key from the URL when the value equals the default:

```ts
export const { $href } = defineTypedHrefWithNuqs<AppRoutes, AppRouteParamsMap>()({
const withNuqs = defineTypedHrefWithNuqs<AppRoutes, AppRouteParamsMap>();

export const { $href } = withNuqs({
"/search": {
q: parseAsString.withDefault(""),
page: parseAsInteger.withDefault(1),
Expand Down
9 changes: 8 additions & 1 deletion packages/next-typed-href/src/nuqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ type ParserValues<Parsers extends Record<string, AnyParserBuilder>> = {
* Routes that have nuqs parsers defined accept typed searchParams values.
* Routes without parsers fall back to standard URLSearchParams input.
*
* The function is curried — call it once with explicit type parameters to get
* a route-specific factory, then call that factory with your parsers map.
* TypeScript cannot infer the third type parameter alongside explicit ones
* (microsoft/TypeScript#26242), so the two-step form is required.
*
* @example
* const { $href } = defineTypedHrefWithNuqs<Routes, RouteParamsMap>()({
* // Recommended: store the factory and call it once
* const withNuqs = defineTypedHrefWithNuqs<Routes, RouteParamsMap>();
* const { $href } = withNuqs({
* "/search": { q: parseAsString, page: parseAsInteger },
* });
*
Expand Down
Loading