【问题标题】:'NestMiddleware<any, any>' is not assignable to parameter of type 'Function | Type<any>''NestMiddleware<any, any>' 不能分配给'Function |键入<任何>'
【发布时间】:2020-06-11 21:11:59
【问题描述】:

我将一个中间件注入到一个模块中。

我在调用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.

【问题讨论】:

    标签: typescript nestjs


    【解决方案1】:

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

    • private middleware: Type&lt;any&gt;

    代替:

    • private middleware: NestMiddleware

    正在从@nestjs/common 导入Type

    【讨论】:

      猜你喜欢
      • 2019-10-05
      • 2021-04-22
      • 2019-04-22
      • 2018-04-23
      • 2018-08-30
      • 2021-12-03
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      相关资源
      最近更新 更多