Type dynamic segment parameters
A Route Handler serves a dynamic resource such as /users/123.
Next.js 16+ · Type Alias
RouteContext is a globally available TypeScript helper for Route Handlers. It derives strongly typed params from a route-pattern literal such as '/users/[id]'.
RouteContext is a globally available TypeScript helper for Route Handlers. Give it a route literal such as '/users/[id]' and the context parameter gains a route-specific params type; read params asynchronously before constructing the response.
RouteContext<'/route-pattern'>| Name | Type | Description |
|---|---|---|
route | string literal | A route-pattern string literal used to derive the handler's parameter types. |
ctx | RouteContext<route> | The Route Handler context typed with RouteContext for the route pattern. |
No standalone runtime value; RouteContext supplies the compile-time type of a Route Handler context.
next typegen.export async function GET(
_request: Request,
context: RouteContext<'/users/[id]'>
) {
const { id } = await context.params
return Response.json({ id })
}A dynamic route handler receives strongly typed params through the globally available helper.
A Route Handler serves a dynamic resource such as /users/123.
A handler processes nested documentation or file paths.
A project wants route-derived types without maintaining manual parameter interfaces.
RouteContext types a Route Handler's context parameter from a route literal. For '/users/[id]', it provides a params type containing id, and params must be awaited.
Follow-up: How would you type a catch-all route? Use a literal such as '/docs/md/[...slug]'; the generated params type reflects the catch-all segment.
It is globally available after route type generation, so no import is required. Generation runs during next dev, next build, or next typegen.
Follow-up: What happens in a fresh checkout before generation? Run next typegen, next dev, or next build so the global helper is produced.
Pass the exact App Router route pattern as its generic argument, then accept RouteContext<that pattern> as the handler context. Await context.params to access dynamic values.
Follow-up: Why use the route pattern instead of a hand-written params object? The route literal lets Next.js derive parameter names and shapes from the route structure.
Route HandlersPagePropsLayoutPropsnext typegendynamic segments