Mongoose post hook TypeScript 中的类型错误

Mongoose post hook Type error in TypeScript

我正在使用猫鼬和打字稿,当我在我想使用的模型上调用 updateOne() 时

someSchema.post<Query>('updateOne', function(doc: IsomeDoc) {
    console.log(this)
}

问题是 this 是查询类型,如果我抑制打字稿检查器并忽略它给我一个错误:

Generic type 'Query<ResultType, DocType, THelpers>' requires between 2 and 3 type arguments.

这是我的架构

const someSchema = new Schema(
  {
    _id: { type: String, required: true },
    status: { type: String },
  },
  { timestamps: true }
)

如何在函数中获取 this 的正确类型?经过大量搜索后,几乎没有使用 post hook with typescript and mongoose。 什么是结果类型?

编辑:

在看到@gregooroo 的回答后,我可以通过查询 Query<IsomeDoc, IsomeDoc> 但它没有给我这个对象的正确类型。

对于查询中间件,您需要构造一个查询类型,该类型具有此查询应该具有的通用类型return


someSchema.post<Query<IsomeDoc, IsomeDoc>>('updateOne', function(doc) {
    console.log(this)
}