How to extend Request object in Express with TypeScript
Hi everyone! I'm writing a backend on Express with TypeScript and encountered a problem with typing the Request object.
I have a middleware that adds user data to the request after authentication. I'm doing something like this:
```typescript
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
interface UserPayload {
id: string;
email: string;
role: 'admin' | 'user';
}
export const authMiddleware = (req: Request, res: Response, next: NextFunction) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(401).json({ message: 'No token' });
}
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET!) as UserPayload;
req.user = decoded; // TypeScript error: Prope