Course contentsShow
← TypeScript
Lesson 1531 of 1,79635. TypeScript with Framework Cross-ReferencesFree lesson

Typing Request Body and Query in Express

Adding ReqBody and ReqQuery generics to Request<P, ResBody, ReqBody, ReqQuery> for POST data and query strings.

Typing Request Body and Query in Express

What you'll learn: How to add specific types to the third and fourth generic parameters of Express's Request interface to type-check POST data and query strings.

The Problem with Generic Express Requests

So far you've seen how to type route parameters using the first generic parameter of Request<P>. But Express requests carry more than just URL parameters—they also contain request bodies (from POST/PUT operations) and query strings (the ?key=value parts of URLs).

Without explicit types, both req.body and req.query default to any, which means no type safety and no autocomplete.

The Full Request Generic Signature

Express's Request interface actually accepts four generic type parameters:

Request<P, ResBody, ReqBody, ReqQuery>
  • P: Route parameters (covered in the previous lesson)
  • ResBody: Response body type (rarely used directly in request typing)
  • ReqBody: Request body type — what comes from req.body
  • ReqQuery: Query string type — what comes from req.query

Typing the Request Body

When handling POST or PUT requests, you receive data in req.body. Define an interface for it:

interface CreateUserBody {
  username: string;
  email: string;
  age?: number;
}

app.post('/users', (req: Request<{}, {}, CreateUserBody>, res) => {
  const { username, email, age } = req.body; // All typed!
  // username and email are string, age is number | undefined
});

Notice the empty {} for route params and response body—we're only typing the third parameter here.

Typing Query Strings

Query strings are accessed via req.query. Type them the same way:

interface SearchQuery {
  term: string;
  limit?: string; // Query params are always strings!
  page?: string;
}

app.get('/search', (req: Request<{}, {}, {}, SearchQuery>, res) => {
  const { term, limit, page } = req.query; // All typed!
  // Remember: limit and page are strings, not numbers
});

Important: Query parameters are always strings because they come from the URL. If you need numbers, parse them explicitly.

Combining All Four Generics

You can type everything at once:

interface UserParams {
  userId: string;
}

interface UpdateUserBody {
  email: string;
}

interface UpdateQuery {
  notify?: string; // "true" or "false" as string
}

app.put(
  '/users/:userId',
  (req: Request<UserParams, {}, UpdateUserBody, UpdateQuery>, res) => {
    const { userId } = req.params;        // string
    const { email } = req.body;           // string
    const { notify } = req.query;         // string | undefined
  }
);

Now TypeScript knows the exact shape of your params, body, and query—all from that single type annotation.

Key Takeaway: Use the third and fourth generic parameters of Request<P, ResBody, ReqBody, ReqQuery> to type req.body and req.query respectively, bringing full type safety to your Express request handling.