NodeJS Express TypeScript路由属性

y3bcpkx1  于 9个月前  发布在  Node.js
关注(0)|答案(2)|浏览(86)

我试图使用TypeScript在Express中创建一个路由,所以我像往常一样,将createUser函数的参数设置为reqRequestresResponsenextNextFunction,显然是从@types/express开始的。每次都能工作,但这次它显示错误:

No overload matches this call.
  Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error.
    Argument of type '(req: Request, res: Response, next: any) => Promise<any>' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
      Types of parameters 'req' and 'req' are incompatible.
        Type 'Request<ParamsDictionary, any, any, ParsedQs>' is missing the following properties from type 'Request': cache, credentials, destination, integrity, and 15 more.
  Overload 2 of 3, '(path: PathParams, ...handlers: RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error.
    Argument of type '(req: Request, res: Response, next: any) => Promise<any>' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>'.
      Type '(req: Request, res: Response, next: any) => Promise<any>' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
  Overload 3 of 3, '(path: PathParams, subApplication: Application): Router', gave the following error.
    Argument of type '(req: Request, res: Response, next: any) => Promise<any>' is not assignable to parameter of type 'Application'.
      Type '(req: Request, res: Response, next: any) => Promise<any>' is missing the following properties from type 'Application': init, defaultConfiguration, engine, set, and 61 more.

字符串
如何解决这个问题?下面是应用程序的一些代码:

protected setupRoutes(): void {
    this.router.post("/", this.createUser);
  }

  // Routes:
  private async createUser(req: Request, res: Response, next): Promise<any> {
    try {
      res.status(200).send("Hi!");
    } catch (err) {
      return next({
        status: err.status,
        message: err.message,
      });
    }
  }

piv4azn7

piv4azn71#

我很惊讶这对你有用。Express有自己的类型可用于请求和响应,它们扩展了你正在使用的基本类型。
例如,下面是type definition of the express request

interface Request<P = core.ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = core.Query> extends core.Request<P, ResBody, ReqBody, ReqQuery> { }

字符串
尝试使用这些类型:

import express from 'express';

const handleMyRequest = (req: express.Request, res: express.Response) => {
  // Whatever
}

js4nwp54

js4nwp542#

我使用express.Requestexpress.Response来解决这个问题

import { IUserDTO } from "../entities/User";
import { RegisterUserUseCases } from "./registerUserUseCases"
import express from 'express'

class RegisterUserController {
    constructor(private useCase:RegisterUserUseCases){}

    async handle(req: express.Request,res: express.Response):Promise<express.Response> {

        const { email, name, password, phone } : IUserDTO | any = req.body;

        await this.useCase.execute({
            email,
            name,
            password,
            phone
        })

        return res.status(201).json({ message: 'Registo criado com sucesso' })
    }
}

export { RegisterUserController }

字符串

相关问题