Unverified Commit a94f5a54 authored by Dima Voytenko's avatar Dima Voytenko Committed by GitHub

Update useParams typing to assume a given shape (#55126)

Alternative to this is user code constantly doing things like `useParams() as { x: string }`.
parent 14145c85
......@@ -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
}
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment