What You'll Learn
You'll learn how to use TypeScript's generic Request<Params> type to make route parameters like req.params.id type-safe at compile time, catching typos and mismatches before runtime.
The Problem with Untyped Route Parameters
In Express, you access URL parameters through req.params, but by default TypeScript treats this as a plain object with string values. This means you get no autocomplete and no errors when you misspell parameter names:
app.get('/users/:userId', (req, res) => {
const id = req.params.usrId; // Typo! No error, returns undefined
});
Using Generic Request<Params>
Express's Request type accepts up to four generic type arguments, with the first one for route parameters. You define an interface describing your URL parameters and pass it as the first generic argument:
interface UserRouteParams {
userId: string;
}
app.get('/users/:userId', (req: Request<UserRouteParams>, res) => {
const id = req.params.userId; // Type-safe! Autocomplete works
const wrong = req.params.usrId; // Error: Property 'usrId' does not exist
});
Important: All route parameters are typed as string because that's what Express provides—even numeric IDs arrive as strings.
Multiple Parameters Example
For routes with multiple parameters, include all of them in your interface:
interface PostRouteParams {
userId: string;
postId: string;
}
app.get('/users/:userId/posts/:postId', (req: Request<PostRouteParams>, res) => {
const { userId, postId } = req.params; // Both are string and type-checked
});
Why This Matters
Think of route parameters like a contract between your URL pattern and your handler code. Without typing, you're trusting yourself to remember the exact parameter names. With Request<Params>, TypeScript enforces that contract—if you rename a route parameter, TypeScript immediately shows you every place in your handler that needs updating.
Key Takeaway: Using Request<ParamsInterface> gives you compile-time safety for URL parameters, turning runtime undefined errors into immediate type errors you can fix before shipping.