'NestMiddleware <any,any>'不可分配给'Function |输入<any>'

时间:2020-06-11 13:11:59

标签: typescript nestjs

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

致电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.

1 个答案:

答案 0 :(得分:0)

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

  • private middleware: Type<any>

代替:

  • private middleware: NestMiddleware

Type导入@nestjs/common