Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.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
Javascript typeof EnforceAjax不可分配给类型中间件_Javascript_Typescript - Fatal编程技术网

Javascript typeof EnforceAjax不可分配给类型中间件

Javascript typeof EnforceAjax不可分配给类型中间件,javascript,typescript,Javascript,Typescript,我有以下类和接口: export class EnforceAjax implements Middleware { public handle(client: Client) { return client.ajax === true } } export interface Middleware { handle?(client: Client, ...args: any[]): boolean | Response } 当我尝试执行以下操作时: Router.gro

我有以下类和接口:

export class EnforceAjax implements Middleware {
  public handle(client: Client) {
    return client.ajax === true
  }
}

export interface Middleware {
  handle?(client: Client, ...args: any[]): boolean | Response
}
当我尝试执行以下操作时:

Router.group('/api', { middleware: [EnforceAjax] }, async () => require('./api'))


export interface RouterOptions {
  middleware?: (Middleware | string)[]
}

public static group(routePrefix: string, options: RouterOptions, callback: Function): void
我得到以下错误:

类型“typeof EnforceAjax”不可分配给类型“string | Middleware”。 类型为“typeof EnforceAjax”的值与类型为“Middleware”的值没有共同的属性。你想叫它吗


当我添加
new
关键字时,错误消失了,但我不想将类的实例作为值传递到数组中。

然后键入
中间件作为中间件构造函数:

 export interface RouterOptions {
   middleware?: ({ new(): Middleware; } | string)[]
 }

不知道为什么需要类。

好吧,
的静态方法EnforceAjax
与接口
中间件
不匹配,只有实例方法匹配。因此,是的,您需要传递一个实例。值
EnforceAjax
不是
中间件
,而是在
新建
时生成
中间件
的构造函数。如果您打算
EnforceAjax
成为
中间件
可能您想要
句柄()
要成为
静态的
?我使用一个类,因为一些中间件类可能会稍微复杂一些。