AI News Hub Logo

AI News Hub

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

DEV Community
Chukwuemeka Ngumoha

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. You need to extend Express’ Request interface using declaration merging. src/types/express/index.d.ts 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 to declare namespace Express. The export {} is important — without it, TypeScript may not properly recognize the file. tsconfig.json Make sure TypeScript can see your custom types: { "files": ["./src/types/express/index.d.ts"], "compilerOptions": { ... "typeRoots": [ "./node_modules/@types", "./src/types" ], }, ... } Sometimes VS Code and nodemon cache old types. Restart your TS server Stop and restart your dev server (e.g nodemon) Result Now this works cleanly: req.user = decoded; No IntelliSense errors. No compiler complaints. 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 !!!