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'
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
Step 2: Add this definition
import { JwtPayload } from "jsonwebtoken";
declare module 'express-serve-static-core' {
export interface Request {
user ?: string | JwtPayload,
}
}
export {};
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 todeclare 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"
],
},
...
}
Step 4: Restart TypeScript + Dev Server
Sometimes VS Code and nodemon cache old types.
Result
Now this works cleanly:
req.user = decoded;
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)