Skip to content

Commit

Permalink
Update useParams typing to assume a given shape (#55126)
Browse files Browse the repository at this point in the history
Alternative to this is user code constantly doing things like `useParams() as { x: string }`.
  • Loading branch information
dvoytenko authored Sep 14, 2023
1 parent 14145c8 commit a94f5a5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/next/navigation-types/compat/navigation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ declare module 'next/navigation' {
*
* If used from `pages/`, the hook will return `null`.
*/
export function useParams(): Record<string, string | string[]> | null
export function useParams<
T extends Record<string, string | string[]> = Record<
string,
string | string[]
>
>(): T | null
}
4 changes: 2 additions & 2 deletions packages/next/src/client/components/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 extends Params = Params>(): 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
Expand Down

0 comments on commit a94f5a5

Please sign in to comment.