首页 > 解决方案 > 没有重载匹配此调用错误(Typescript/Express)

问题描述

我创建了一个控制器:

import { Request, Response } from "express";

export const signIn = async (req: Request, res: Response) => {
  try {
    await res.send("This is the signIn route");
  } catch (err) {
    return res.status(400).send(err);
  }
};

export default signIn;

然后我将控制器导出到我的路由处理程序中:

import express from "express";
const router = express.Router();

import signIn from "../controller/signin";
router.post("/signIn", signIn);

我的 index.ts 文件:

import express from 'express'
import authRoutes from '../src/routes/auth-routes'
import errorHandler from '../src/middlewares/error-handler'
import {connectDB} from '../src/database/db'


const app = express()
connectDB()

const port = 3005

// @Body parser
app.use(express.json())

// @Routes
app.use('/api/users', authRoutes)
app.use(errorHandler)

// @Listening port
app.listen(port, () => {
    console.log(`*********Server listening on port ${port}*******`)
   
})

但是,我收到了这个错误:

{
    "resource": "/C:/Projects/myAppName/auth/src/routes/auth-routes.ts",
    "owner": "typescript",
    "code": "2769",
    "severity": 8,
    "message": "No overload matches this call.\n  Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>[]): Router', gave the following error.\n    Argument of type '(req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.\n      Types of parameters 'req' and 'req' are incompatible.\n        Type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is missing the following properties from type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>': RequestValidationError, DatabaseConnectionError\n  Overload 2 of 3, '(path: PathParams, ...handlers: RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>[]): Router', gave the following error.\n    Argument of type '(req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.\n      Type '(req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.\n  Overload 3 of 3, '(path: PathParams, subApplication: Application): Router', gave the following error.\n    Argument of type '(req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>' is not assignable to parameter of type 'Application'.\n      Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>) => Promise<...>' is missing the following properties from type 'Application': init, defaultConfiguration, engine, set, and 61 more.",
    "source": "ts",
    "startLineNumber": 12,
    "startColumn": 24,
    "endLineNumber": 12,
    "endColumn": 30
}

我不明白为什么我得到这个“没有重载匹配这个调用错误”。请有人可以看看我上面的代码并帮助我吗?我会非常感激的。

标签: node.jstypescriptexpress

解决方案


推荐阅读