Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Route type for router APIs #47931

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion packages/next/src/build/webpack/plugins/next-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,32 @@ declare module 'next/link' {
}

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')
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/shared/lib/app-router-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export type CacheNode =
*/
parallelRoutes: Map<string, ChildSegmentMap>
}
interface NavigateOptions {

export interface NavigateOptions {
forceOptimisticNavigation?: boolean
}

Expand Down
15 changes: 15 additions & 0 deletions test/integration/app-types/app-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
// Can't export arbitrary things.
expect(errors).toContain(`"foo" is not a valid Page export field.`)
Expand Down
25 changes: 25 additions & 0 deletions test/integration/app-types/src/app/type-checks/router/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'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} />
}