Express में TypeScript के साथ Request ऑब्जेक्ट को कैसे बढ़ाएं
सभी को नमस्ते! मैं Express के साथ TypeScript का उपयोग करके बैकएंड लिख रहा हूं और Request ऑब्जेक्ट की टाइपिंग की समस्या का सामना कर रहा हूं।
मेरे पास एक middleware है जो प्रमाणीकरण के बाद अनुरोध में उपयोगकर्ता डेटा जोड़ता है। मैं कुछ इस तरह कर रहा हूं:
```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: 'कोई टोकन नहीं' });
}
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET!) as UserPayload;
req.user = decoded; // TypeScript त्रुटि: Prope