DEV Community

Chukwuemeka Ngumoha
Chukwuemeka Ngumoha

Posted on

Fixing “Property 'user' does not exist on type 'Request'” in Express + TypeScript

If you’ve ever tried adding req.user inside Express middleware while using TypeScript, you’ve probably run into this error:

Property 'user' does not exist on type 'Request'
Enter fullscreen mode Exit fullscreen mode

I hit this while building authentication middleware for my API with JWT and Prisma.

The issue is simple: Express’ default Request type only knows about built-in properties like body, params, and headers. It has no idea about your custom user property.

The Fix

You need to extend Express’ Request interface using declaration merging.

Step 1: Create a type declaration file

src/types/express/index.d.ts
Enter fullscreen mode Exit fullscreen mode

Step 2: Add this definition

import { JwtPayload } from "jsonwebtoken";

declare module 'express-serve-static-core' {
  export interface Request {
    user ?: string | JwtPayload,
  }
}

export {};
Enter fullscreen mode Exit fullscreen mode

NOTES:

  • declare module 'express-serve-static-core' is relevant if you're using the newer versions of express in your project. Otherwise, change that line to declare namespace Express.
  • The export {} is important — without it, TypeScript may not properly recognize the file.

Step 3: Update tsconfig.json

Make sure TypeScript can see your custom types:

{
  "files": ["./src/types/express/index.d.ts"],
  "compilerOptions": {
    ...
    "typeRoots": [
      "./node_modules/@types",
      "./src/types"
    ],
  },
  ...
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Restart TypeScript + Dev Server

Sometimes VS Code and nodemon cache old types.

  • Restart your TS server
    VS code command palette: Restart TS server.

  • Stop and restart your dev server (e.g nodemon)

Result

Now this works cleanly:

req.user = decoded;
Enter fullscreen mode Exit fullscreen mode

No IntelliSense errors. No compiler complaints.

Final Note

This issue looks small, but understanding declaration merging is one of those TypeScript moments that makes backend development much smoother. Anyway, I hope this was helpful. Feel free to leave your thoughts in the comments. Have fun building !!!

Top comments (0)