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
成为中间件可能您想要句柄()
要成为静态的
?我使用一个类,因为一些中间件类可能会稍微复杂一些。