【问题标题】:How can i extend mongoose Query with typescript?如何使用打字稿扩展猫鼬查询?
【发布时间】:2019-10-06 00:00:57
【问题描述】:

我正在尝试将这段代码改编为打字稿:https://github.com/kephin/Node_Redis-Caching/blob/master/services/cache.js ,但我无法扩展 Query 对象。

目前用声明合并扩展它,就像我用 express 做的一样,但在这种情况下它不起作用

declare namespace Mongoose {
  interface Query {
    cache: any;
  }

【问题讨论】:

标签: typescript express mongoose


【解决方案1】:

我设法让它像这样工作:

# mongoose.d.ts

type CacheOptions = { key?: string }

declare module 'mongoose' {
  interface DocumentQuery<T, DocType> {
    cache(options?: CacheOptions): Query<T>
    useCache: boolean
    hashKey: string
  }
}

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 2019-04-06
    • 1970-01-01
    • 2016-02-05
    • 2020-05-11
    • 2020-10-29
    • 2016-06-14
    • 2016-04-01
    • 1970-01-01
    相关资源
    最近更新 更多