TypeScript开发MERN项目遇No overload matches this call错误求助
我正在用TypeScript开发MERN栈Web项目,authRoutes.ts的第7行和第10行出现TypeScript类型错误,以下是相关代码和错误详情:
相关代码
authRoutes.ts
import { Router } from "express"; import { register, login } from "./../controllers/authController"; const router = Router(); // Register route router.post("/register", register); // Login route router.post("/login", login); export default router;
authController.ts
import { NextFunction, Request, Response } from "express"; import User from "../models/userModel"; // Import the User model // Register a new user export const register = async ( req: Request, res: Response, next: NextFunction ): Promise<Response> => { try { const { name, email, password, isAdmin } = req.body; // Check if the user already exists const userExists = await User.findOne({ email }); if (userExists) { return res.status(400).json({ message: "User already exists" }); } // Create a new user and save to the database const user = new User({ name, email, password, isAdmin }); await user.save(); return res.status(201).json({ message: "User created successfully" }); } catch (err) { console.error(err); return res.status(500).json({ message: "Server error" }); } }; // Login user export const login = async ( req: Request, res: Response, next: NextFunction ): Promise<Response> => { try { const { email, password } = req.body; // Find user by email const user = await User.findOne({ email }); if (!user) { return res.status(400).json({ message: "Invalid credentials" }); } // Simple password check (no bcrypt for now) if (user.password !== password) { return res.status(400).json({ message: "Invalid credentials" }); } return res.status(200).json({ message: "Login successful" }); } catch (err) { console.error(err); return res.status(500).json({ message: "Server error" }); } };
错误信息
No overload matches this call.
The last overload gave the following error.
Argument of type '(req: Request, res: Response, next: NextFunction) => Promise' is not assignable to parameter of type 'Application<Record<string, any>>'.
Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<...>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.ts(2769)
index.d.ts(168, 5): The last overload is declared here.
这是我的学习项目,目前对相关知识掌握不足,尝试过一些方法但都无效,请求帮忙解决这个错误。
内容的提问来源于stack exchange,提问作者humam altamash

