Unverified Commit 89e30409 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Implement `Route` type for router APIs (#47931)

This pull request extends the type checking functionality of the
`typedRoutes` feature, which is currently available in the `<Link
href={Route}>` component, to the following `router` APIs:
`router.push()`, `router.prefetch()`, and `router.replace()`. The
implementation leverages the existing work done for the `<Link>`
component, allowing for a straightforward integration of type checking
into the aforementioned `router` APIs.

fix NEXT-501
parent 64665380
...@@ -432,7 +432,32 @@ declare module 'next/link' { ...@@ -432,7 +432,32 @@ declare module 'next/link' {
} }
export default function Link<RouteType>(props: LinkProps<RouteType>): JSX.Element export default function Link<RouteType>(props: LinkProps<RouteType>): JSX.Element
}` }
declare module 'next/navigation' {
export * from 'next/dist/client/components/navigation'
import type { NavigateOptions, AppRouterInstance as OriginalAppRouterInstance } from 'next/dist/shared/lib/app-router-context'
interface AppRouterInstance extends OriginalAppRouterInstance {
/**
* Navigate to the provided href.
* Pushes a new history entry.
*/
push<RouteType>(href: __next_route_internal_types__.RouteImpl<RouteType>, options?: NavigateOptions): void
/**
* Navigate to the provided href.
* Replaces the current history entry.
*/
replace<RouteType>(href: __next_route_internal_types__.RouteImpl<RouteType>, options?: NavigateOptions): void
/**
* Prefetch the provided href.
*/
prefetch<RouteType>(href: __next_route_internal_types__.RouteImpl<RouteType>): void
}
export declare function useRouter(): AppRouterInstance;
}
`
} }
const appTypesBasePath = path.join('types', 'app') const appTypesBasePath = path.join('types', 'app')
......
...@@ -63,7 +63,8 @@ export type CacheNode = ...@@ -63,7 +63,8 @@ export type CacheNode =
*/ */
parallelRoutes: Map<string, ChildSegmentMap> parallelRoutes: Map<string, ChildSegmentMap>
} }
interface NavigateOptions {
export interface NavigateOptions {
forceOptimisticNavigation?: boolean forceOptimisticNavigation?: boolean
} }
......
...@@ -44,6 +44,21 @@ describe('app type checking', () => { ...@@ -44,6 +44,21 @@ describe('app type checking', () => {
) )
}) })
it('should generate route types correctly and report router API errors', async () => {
// Make sure all errors were reported and other links passed type checking
const errorLines = [
...errors.matchAll(
/\.\/src\/app\/type-checks\/router\/page\.tsx:(\d+):/g
),
].map(([, line]) => +line)
const ST = 11
const ED = 13
expect(errorLines).toEqual(
Array.from({ length: ED - ST + 1 }, (_, i) => i + ST)
)
})
it('should type check invalid entry exports', () => { it('should type check invalid entry exports', () => {
// Can't export arbitrary things. // Can't export arbitrary things.
expect(errors).toContain(`"foo" is not a valid Page export field.`) expect(errors).toContain(`"foo" is not a valid Page export field.`)
......
'use client'
import { useRouter } from 'next/navigation'
import type { Route } from 'next'
export default function Page() {
const router = useRouter()
function test() {
// Invalid routes:
router.push('/wrong-link')
router.push('/blog/a?1/b')
router.push(`/blog/${'a/b/c'}`)
// Correctly typed:
router.push('/dashboard/another')
router.prefetch('/about')
router.push('/redirect')
router.push(`/blog/${'a/b'}`)
router.push('/invalid' as Route)
router.back()
}
return <div onClick={test} />
}
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