node.js - 在 mongoose .d.ts 中分配自定义方法,这样我就可以在 typescript 中使用 mongoose 的任何地方使用它们

标签 node.js typescript mongoose types

我正在尝试在 mongoose Api Hook 中创建自定义函数,但该函数未定义,因为它尚未声明。我已经创建了 index.d.ts 单独的文件,但不知道如何添加它。

const exec = mongoose.Query.prototype.exec;

mongoose.Query.prototype.cache = function (options:ICacheOptions = {}) {
  this.useCache = true;
  this.hashKey = JSON.stringify(options.key || "");
  return this;
}

链接::cache.ts !

错误是:类型“查询”上不存在属性“缓存”。您指的是“catch”吗?

我尝试过的:我创建了 .d.ts 文件并尝试声明它们。

declare module 'mongoose' {
  interface DocumentQuery<T,  DocType extends import("mongoose").Document, QueryHelpers = {}>{
    mongooseCollection: {
      name: any;
    };
    cache():DocumentQuery<T[], Document> & QueryHelpers;
    useCache: boolean;
    hashKey: string;

  }
}

链接::index.d.ts !

我的错误是::

src/lib/services/cache.ts(16,26): error TS2551: Property 'cache' does not exist on type 'Query<any>'. Did you mean 'catch'?
src/lib/services/cache.ts(17,8): error TS2339: Property 'useCache' does not exist on type 'Query<any>'.
src/lib/services/cache.ts(18,8): error TS2339: Property 'hashKey' does not exist on type 'Query<any>'.
src/lib/services/cache.ts(25,12): error TS2339: Property 'useCache' does not exist on type 'Query<any>'.
src/lib/services/cache.ts(30,41): error TS2339: Property 'mongooseCollection' does not exist on type 'Query<any>'.
src/lib/services/cache.ts(33,52): error TS2339: Property 'hashKey' does not exist on type 'Query<any>'.
src/lib/services/cache.ts(45,22): error TS2339: Property 'hashKey' does not exist on type 'Query<any>'.
src/lib/services/cache.ts(46,24): error TS2339: Property 'hashKey' does not exist on type 'Query<any>'.

我想以某种方式解决这个问题,并且还想知道如何扩展 .d.ts 中的某些属性(如果它们是类)。提前致谢。

最佳答案

你的增强确实有效。它需要放置在您已配置 TypeScript 来查找源文件的位置。 declare module类型定义的风格称为“环境声明”,这意味着您不必将其放在任何特定的目录或文件中。它甚至可以是常规的 .ts文件。最简单的方法是将声明放入 cache.ts ,与您分配 mongoose.Query.prototype.cache 的同一文件.

编辑:我忘记解决您关于增强类的具体问题。正如您所猜测的,您可以使用 interface 来扩充类。关键词。这是因为在 TypeScript 中定义一个类会做两件事:它定义一个值(调用时调用的构造函数,例如 new DocumentQuery ),以及该类实例的类型(实际上是一个接口(interface))。值和类型都具有相同的名称。因为类的类型部分是一个接口(interface),所以您可以像任何其他接口(interface)一样扩展它。

因此,在这种情况下,您将增强 DocumentQuery type,它是 Query 的父类(super class),但您分配 cache方法Query原型(prototype)。这是有效的,因为 Query延伸DocumentQuery ,所以当您声明 DocumentQuery 时现在有一个cache TypeScript 方法假定子类,包括 Query ,也有同样的方法。这确实会导致差异:TypeScript 现在假设 DocumentQuery实例有 cache方法,但您实际上只为 Query 定义了该方法。更改类型声明以增强 Query 会更准确。而不是DocumentQuery ,或将方法实现分配给 DocumentQuery.prototype而不是Query.prototype .

关于node.js - 在 mongoose .d.ts 中分配自定义方法,这样我就可以在 typescript 中使用 mongoose 的任何地方使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57232323/

相关文章:

node.js - MongoDB 更新子文档更改其_id

javascript - 比较 Express User 与 Mongoose 外键

node.js - 在整个 Node 项目上使用 UglifyJs?

node.js - 使用 sequelize 时 node-mysql 是否可用

javascript - typescript 创建来自变量的类型的对象

javascript - Angular 2 : Typescript : Filter grid data based on provided filter condition

Node.js - 如何计算 mongodb 中子文档的聚合(总和)?

node.js - 我在使用 Node.js 引用 MongoDB 中的嵌套子文档时遇到问题

node.js - 在 Express 中捕获非法 JSON POST 数据?

javascript - 为什么 TypeScript 有时只能通过字符串索引对象