简体   繁体   English

“void”类型的参数不可分配给“PathParams”类型的参数

[英]Argument of type 'void' is not assignable to parameter of type 'PathParams'

Created route handler and while adding in my route .创建路由handler并添加我的route

import {Application, NextFunction} from 'express';
import {container} from 'tsyringe';
const routeConstantsArray  = { };

const constants: any  = []
constants.push(routeConstantsArray)
let next : NextFunction;
export const loadConstantsMiddleware = (app:Application) => {
    app.locals = constants
    next();
};

Route路线

import express from 'express';
import {Application} from 'express';
import cors from 'cors';
// Import Internal Modules
import {loadConstantsMiddleware} from '../core/middleware/constants_middleware';
 const startupRouterConfig = (app: Application): Application => {
    app.use(cors());
    app.use(express.json());
    app.use(loadConstantsMiddleware(app));
    return app;
};

Error错误
(parameter) app: express.Application No overload matches this call. (参数) app: express.Application没有重载匹配这个调用。 The last overload gave the following error:最后一个重载给出了以下错误:

Argument of type 'void' is not assignable to parameter of type 'PathParams'.ts(2769)
index.d.ts(55, 5): The last overload is declared here.

To use the loadConstantsMiddleware as middleware, you have to return a request handler, like:要将loadConstantsMiddleware用作中间件,您必须返回一个请求处理程序,例如:

export const loadConstantsMiddleware = (app:Application) => {
    app.locals = constants;
    return (req, res, next) => {
      next();
    }
};

But in this scenario, it probably doesn't make sense to use it as a middleware since the constants are scoped to the entire application (versus just the request).但是在这种情况下,将其用作中间件可能没有意义,因为常量的范围是整个应用程序(而不是请求)。

To just add it to the app directly, without middleware, you can do:要直接将其添加到应用程序中,无需中间件,您可以执行以下操作:

// main
app.use(cors());
app.use(express.json());
loadConstantsMiddleware(app);

// load-constants-middleware
export const loadConstantsMiddleware = (app:Application) => {
  app.locals = constants
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “NextHandleFunction”类型的参数不可分配给“PathParams”类型的参数 - Argument of type 'NextHandleFunction' is not assignable to parameter of type 'PathParams' '(req: Request, res: IResponse, next: NextFunction) => void' 类型的参数不能分配给带有 express.js 的 'PathParams' 类型的参数 - Argument of type '(req: Request, res: IResponse, next: NextFunction) => void' is not assignable to parameter of type 'PathParams' with express.js 带有 TS: void' 的 Express 中间件不可分配给“PathParams”类型的参数 - Express middleware with TS: void' is not assignable to parameter of type 'PathParams' “(事件:字符串)=&gt; void”类型的参数不可分配给“EventListenerOrEventListenerObject”类型的参数 - Argument of type '(event: string) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject 承诺:void类型的参数不能分配给T类型 - Promise: Argument of type void is not assignable to type T [type]类型的参数不能分配给[type]类型的参数 - Argument of type [type] is not assignable to parameter of type [type] “this”类型的参数不能分配给参数“Construct” - Argument of type 'this' not assignable to parameter 'Construct' '(res: Response 类型的参数<any, record<string, any> >,下一个:NextFunction)=> Promise<void> ' 不可分配给类型的参数</void></any,> - Argument of type '(res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>' is not assignable to parameter of type 'Promise 类型的参数<unknown> ' 不可分配给 'void' 类型的参数.ts(2345) - mockReturnValueOnce</unknown> - Argument of type 'Promise<unknown>' is not assignable to parameter of type 'void'.ts(2345) - mockReturnValueOnce 参数类型“Y”不能赋值参数类型“Y” - argument type “X” is not assignable to parameter type “Y”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM