Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Express类型脚本路由属性_Node.js_Typescript_Express - Fatal编程技术网

Node.js Express类型脚本路由属性

Node.js Express类型脚本路由属性,node.js,typescript,express,Node.js,Typescript,Express,我正在尝试使用TypeScript在Express中创建一个路由,因此我像往常一样,将createUser函数的参数设置为req:Request,res:Response和next,显然是从@types/Express。每次都有效,但这次显示错误: No overload matches this call. Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, a

我正在尝试使用TypeScript在Express中创建一个路由,因此我像往常一样,将
createUser
函数的参数设置为
req
Request
res
Response
next
,显然是从
@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.
没有与此调用匹配的重载。
重载第1个(共3个),(路径:PathParams,…处理程序:RequestHandler[]):Router',出现以下错误。
类型为“(req:Request,res:Response,next:any)=>Promise”的参数不能分配给类型为“RequestHandler”的参数。
参数'req'和'req'的类型不兼容。
类型“Request”缺少类型“Request”中的以下属性:缓存、凭据、目标、完整性等。
重载2/3'(路径:PathParams,…处理程序:RequestHandlerParams[]):Router'出现以下错误。
类型为“(req:Request,res:Response,next:any)=>Promise”的参数不能分配给类型为“RequestHandlerParams”的参数。
类型“(请求:请求,响应:响应,下一步:任何)=>Promise”不可分配给类型“RequestHandler”。
重载3/3'(路径:PathParams,子应用程序:Application):Router',出现以下错误。
类型为“(req:Request,res:Response,next:any)=>Promise”的参数不能分配给类型为“Application”的参数。
类型“(req:Request,res:Response,next:any)=>Promise”缺少类型“Application”中的以下属性:init、defaultConfiguration、engine、set等。
如何解决这个问题? 以下是该应用程序的一些代码:

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,
      });
    }
  }
protected setupRoutes():void{
this.router.post(“/”,this.createUser);
}
//路线:
私有异步createUser(req:Request,res:Response,next):承诺{
试一试{
res.status(200)。发送(“Hi!”);
}捕捉(错误){
下一个返回({
状态:err.status,
message:err.message,
});
}
}

我很惊讶这对你有任何帮助。Express有自己的类型可用于扩展所使用的基本类型的请求和响应

例如,以下是:


作为旁注,如果您计划在
createUser
中使用
this
,最好将侦听器设置为
this.createUser.bind(this)
而不是
this.createUser
。当然,谢谢!我忘了。:>我已经用过了,但是忘了从Express类型导入。现在它工作得很好。谢谢。:)@Cholewka很乐意帮忙:)
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
}