feat: automatically generate route types (#81396)
## Overview
Generates `.next/types/routes.d.ts` file that provides typed route
definitions and global `PageProps`/`LayoutProps` types for App Router
components, eliminating the need for manual imports and providing full
type safety for routing.
## Generated Type Structure
```typescript
// Route union types
type AppRoutes = "/" | "/blog/[slug]" | "/dashboard/[...path]"
type LayoutRoutes = "/" | "/dashboard"
type PageRoutes = never // for pages directory
type RedirectRoutes = "/old-path"
type RewriteRoutes = "/api/proxy/[...path]"
// Parameter mapping with full dynamic route support
type ParamMap = {
"/": {}
"/blog/[slug]": { "slug": string }
"/dashboard/[[...path]]": { "path"?: string[] } // optional catch-all
"/api/[...segments]": { "segments": string[] } // required catch-all
}
// Parallel route slots for layouts
type LayoutSlotMap = {
"/dashboard": "analytics" | "team"
"/": never
}
```
## Usage Examples
```typescript
// Page component - no imports needed, full parameter typing
export default function BlogPost(props: PageProps<'/blog/[slug]'>) {
const { slug } = await props.params // slug is typed as string
const searchParams = await props.searchParams // Record<string, string | string[] | undefined>
return <div>Post: {slug}</div>
}
// Layout with parallel routes - automatic slot typing
export default function DashboardLayout(props: LayoutProps<'/dashboard'>) {
return (
<div>
{props.children}
{props.analytics} {/* Typed parallel route slot */}
{props.team} {/* Typed parallel route slot */}
</div>
)
}
// Catch-all routes with array parameters
export default function DocsPage(props: PageProps<'/docs/[...path]'>) {
const { path } = await props.params // path is typed as string[]
return <div>Docs path: {path.join('/')}</div>
}
```
## Key Features
- **Zero imports**: All types are globally available via declaration
merging
- **Route validation**: TypeScript ensures only valid routes can be used
- **Dynamic route support**: Full typing for `[param]`, `[...param]`,
and `[[...param]]` patterns
- **Parameter extraction**: Automatic typing of route parameters based
on file structure
- **Parallel routes**: Automatic slot typing for layouts with `@slot`
directories
- **Custom routes**: Support for `redirects` and `rewrites` from
`next.config.js`
- **IDE integration**: Full auto-completion for route paths and
parameters
## Route Discovery
The system automatically discovers routes from:
- **App Router**: `app/` directory structure with `page.tsx` and
`layout.tsx` files
- **Pages Router**: `pages/` directory structure (legacy support)
- **Redirects**: Custom redirects defined in `next.config.js`
- **Rewrites**: Custom rewrites defined in `next.config.js`
- **Parallel Routes**: `@slot` directories for layout composition
Showing
Please
register
or
sign in
to comment