微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在构造函数参数中使用泛型

如何解决如何在构造函数参数中使用泛型

我有一个类,它的构造函数有 2 个参数。第一个的类型是 Model(来自 mongoose),第二个是 Query Type(express 中的 req.query 的类型是 express-serve-static-core)

import {Query} from "express-serve-static-core"
import {Model,Document} from "mongoose";

这个类看起来像这样


class APIFeatures<T> {
  public query: Model<T extends Document>;
  public queryParams:Query;
  // query -> Model.find()
  // queryParams -> req.query

  constructor(query:Model<T extends Document>,queryParams:Query) {
    this.query = query;
    this.queryParams = queryParams;
  }

但是 tsc 给了我以下错误

src/utils/APIFeatures.ts:10:48 - error TS1005: '?' expected.

10   constructor<T>(query:Model<T extends Document>,queryParams:Query) {
                                                  ~

Found 1 error.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。