Skip to content

Commit

Permalink
Fix next/navigation type augmentation (vercel#66489)
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable authored Jun 3, 2024
1 parent d6e28bc commit 4f2e65b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
36 changes: 16 additions & 20 deletions packages/next/src/client/components/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import { ReadonlyURLSearchParams } from './navigation.react-server'
*
* Read more: [Next.js Docs: `useSearchParams`](https://nextjs.org/docs/app/api-reference/functions/use-search-params)
*/
function useSearchParams(): ReadonlyURLSearchParams {
// Client components API
export function useSearchParams(): ReadonlyURLSearchParams {
const searchParams = useContext(SearchParamsContext)

// In the case where this is `null`, the compat types added in
Expand Down Expand Up @@ -78,13 +79,15 @@ function useSearchParams(): ReadonlyURLSearchParams {
*
* Read more: [Next.js Docs: `usePathname`](https://nextjs.org/docs/app/api-reference/functions/use-pathname)
*/
function usePathname(): string {
// Client components API
export function usePathname(): string {
// In the case where this is `null`, the compat types added in `next-env.d.ts`
// will add a new overload that changes the return type to include `null`.
return useContext(PathnameContext) as string
}

import {
// Client components API
export {
ServerInsertedHTMLContext,
useServerInsertedHTML,
} from '../../shared/lib/server-inserted-html.shared-runtime'
Expand All @@ -107,7 +110,8 @@ import {
*
* Read more: [Next.js Docs: `useRouter`](https://nextjs.org/docs/app/api-reference/functions/use-router)
*/
function useRouter(): AppRouterInstance {
// Client components API
export function useRouter(): AppRouterInstance {
const router = useContext(AppRouterContext)
if (router === null) {
throw new Error('invariant expected app router to be mounted')
Expand Down Expand Up @@ -137,12 +141,14 @@ interface Params {
*
* Read more: [Next.js Docs: `useParams`](https://nextjs.org/docs/app/api-reference/functions/use-params)
*/
function useParams<T extends Params = Params>(): T {
// Client components API
export function useParams<T extends Params = Params>(): T {
return useContext(PathParamsContext) as T
}

/** Get the canonical parameters from the current level to the leaf node. */
function getSelectedLayoutSegmentPath(
// Client components API
export function getSelectedLayoutSegmentPath(
tree: FlightRouterState,
parallelRouteKey: string,
first = true,
Expand Down Expand Up @@ -201,7 +207,8 @@ function getSelectedLayoutSegmentPath(
*
* Read more: [Next.js Docs: `useSelectedLayoutSegments`](https://nextjs.org/docs/app/api-reference/functions/use-selected-layout-segments)
*/
function useSelectedLayoutSegments(
// Client components API
export function useSelectedLayoutSegments(
parallelRouteKey: string = 'children'
): string[] {
const context = useContext(LayoutRouterContext)
Expand Down Expand Up @@ -229,7 +236,8 @@ function useSelectedLayoutSegments(
*
* Read more: [Next.js Docs: `useSelectedLayoutSegment`](https://nextjs.org/docs/app/api-reference/functions/use-selected-layout-segment)
*/
function useSelectedLayoutSegment(
// Client components API
export function useSelectedLayoutSegment(
parallelRouteKey: string = 'children'
): string | null {
const selectedLayoutSegments = useSelectedLayoutSegments(parallelRouteKey)
Expand All @@ -250,18 +258,6 @@ function useSelectedLayoutSegment(
: selectedLayoutSegment
}

// Client components APIs
export {
useSearchParams,
usePathname,
useSelectedLayoutSegment,
useSelectedLayoutSegments,
useParams,
useRouter,
useServerInsertedHTML,
ServerInsertedHTMLContext,
}

// Shared components APIs
export {
notFound,
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/use-params/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"skipLibCheck": false,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
Expand Down

0 comments on commit 4f2e65b

Please sign in to comment.