【问题标题】:How to properly type mongoose referenced document in typescript如何在打字稿中正确键入猫鼬引用的文档
【发布时间】:2021-07-08 13:28:59
【问题描述】:

mongoose.Types.ObjectId 工作正常,但我想使用用户文档类型更好地键入它,所以如果我填充它,它将推断出正确的类型,如profile.user.toObject({getters:true})。我看到使用UserDocument['id'] 的文章,但它会返回any。有没有合适的方法来输入这个?

interface IProfile {
  user: // <- what type to put here (reference to User model)
  bio: string
  jobTitle: string
}

【问题讨论】:

    标签: node.js typescript mongoose


    【解决方案1】:

    根据docs,它应该可以使用PopulatedDoc,但在你的情况下,我得到any作为结果类型。

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2017-09-11
      • 2021-09-26
      • 2017-01-23
      • 2018-08-20
      • 2016-02-05
      • 2020-05-11
      • 2013-08-20
      • 2021-04-18
      相关资源
      最近更新 更多