From 5f311e28cc9bb9bdda1dc467bdd73ddcc194acaf Mon Sep 17 00:00:00 2001 From: dvoytenko Date: Thu, 7 Sep 2023 17:49:54 -0700 Subject: [PATCH 1/2] Update useParams typing to assume a given shape --- packages/next/src/client/components/navigation.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/src/client/components/navigation.ts b/packages/next/src/client/components/navigation.ts index bf6a56100080d..4acaa2b5a702d 100644 --- a/packages/next/src/client/components/navigation.ts +++ b/packages/next/src/client/components/navigation.ts @@ -164,14 +164,14 @@ function getSelectedParams( * Get the current parameters. For example useParams() on /dashboard/[team] * where pathname is /dashboard/nextjs would return { team: 'nextjs' } */ -export function useParams(): Params { +export function useParams(): T { clientHookInServerComponentError('useParams') const globalLayoutRouterContext = useContext(GlobalLayoutRouterContext) if (!globalLayoutRouterContext) { // This only happens in `pages`. Type is overwritten in navigation.d.ts return null! } - return getSelectedParams(globalLayoutRouterContext.tree) + return getSelectedParams(globalLayoutRouterContext.tree) as T } // TODO-APP: handle parallel routes From 35d451feeea3d3bc45c0e0f704bdc0ed32663e2b Mon Sep 17 00:00:00 2001 From: dvoytenko Date: Thu, 14 Sep 2023 12:51:29 -0700 Subject: [PATCH 2/2] add compat types as well --- packages/next/navigation-types/compat/navigation.d.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/next/navigation-types/compat/navigation.d.ts b/packages/next/navigation-types/compat/navigation.d.ts index e98f555b96f3d..3b5d730ef931f 100644 --- a/packages/next/navigation-types/compat/navigation.d.ts +++ b/packages/next/navigation-types/compat/navigation.d.ts @@ -25,5 +25,10 @@ declare module 'next/navigation' { * * If used from `pages/`, the hook will return `null`. */ - export function useParams(): Record | null + export function useParams< + T extends Record = Record< + string, + string | string[] + > + >(): T | null }