Avatar ·

Express में TypeScript के साथ Request ऑब्जेक्ट को कैसे विस्तारित करें

सभी को नमस्कार! मैं Express के साथ TypeScript का उपयोग करके बैकएंड लिख रहा हूँ और Request ऑब्जेक्ट की टाइपिंग की समस्या का सामना कर रहा हूँ।\n\nमेरे पास एक middleware है जो प्रमाणीकरण के बाद अनुरोध में उपयोगकर्ता डेटा जोड़ता है। मैं लगभग इस प्रकार कर रहा हूँ:\n\n```typescript\nimport { Request, Response, NextFunction } from 'express';\nimport jwt from 'jsonwebtoken';\n\ninterface UserPayload {\n id: string;\n email: string;\n role: 'admin' | 'user';\n}\n\nexport const authMiddleware = (req: Request, res: Response, next: NextFunction) => {\n const token = req.headers.authorization?.split(' ')[1];\n \n if (!token) {\n return res.status(401).json({ message: 'कोई टोकन नहीं' });\n }\n\n try {\n const decoded = jwt.verify(token, process.env.JWT_SECRET!) as UserPayload;\n req.user = decoded; // TypeScript त्रुटि: Prope