' 不能分配给 'Function | 类型的参数 类型',typescript,nestjs"/>

首页 > 解决方案 > '巢中间件' 不能分配给 'Function | 类型的参数 类型'

问题描述

我正在将中间件注入到模块中。

我打电话时遇到类型错误consumer.apply(middleware)

代码:

@Module({
  ...
})
export class FooModule implements NestModule {

  @Inject(FOO_MIDDLEWARE)
  private middleware: NestMiddleware;

  public configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(this.middleware)
      .forRoutes('foo');
  }
}

错误:

TS2345: Argument of type 'NestMiddleware<any, any>' is not assignable to parameter of type 'Function | Type<any>'.   Type 'NestMiddleware<any, any>' is missing the following properties from type 'Type<any>': apply, call, bind, prototype, and 5 more.

标签: typescriptnestjs

解决方案


我找到了解决方法。我做了:

  • private middleware: Type<any>

代替:

  • private middleware: NestMiddleware

Type从导入@nestjs/common


推荐阅读