简体   繁体   English

在 mongoose .d.ts 中分配自定义方法,以便我可以在打字稿中使用 mongoose 的任何地方使用它们

[英]Assign custom methods in mongoose .d.ts so I can use them anywhere with mongoose in typescript

I am trying to create a custom function in mongoose Api hooks but the function is not defined as it has not declared.我正在尝试在 mongoose Api hooks 中创建一个自定义函数,但该函数未定义,因为它尚未声明。 I have created index.d.ts separate file but don't know how to add that.我创建了 index.d.ts 单独的文件,但不知道如何添加。

const exec = mongoose.Query.prototype.exec; const exec = mongoose.Query.prototype.exec;

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

link :: cache.ts !链接 :: cache.ts

error is: Property 'cache' does not exist on type 'Query'.错误是: “查询”类型上不存在属性“缓存”。 Did you mean 'catch'?您指的是 'catch' 吗?

what I have tried: I created .d.ts file and try to declare them.我尝试过的:我创建了 .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;

  }
}

link:: index.d.ts !链接:: index.d.ts

My Errors are ::我的错误是 ::

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>'.

I want somehow I can fix this and also want to know how can I extend some property in .d.ts if they are class.我想以某种方式解决这个问题,还想知道如果它们是类,我如何在 .d.ts 中扩展一些属性。 Thanks in advance.提前致谢。

Your augmentation does work.你的增强确实有效。 It needs to be placed somewhere that you have configured TypeScript to look for source files.它需要放置在您已配置 TypeScript 以查找源文件的某个位置。 The declare module style of type definition is called an "ambient declaration" which means that you don't have to put it in any particular directory or file.类型定义的declare module样式称为“环境声明”,这意味着您不必将其放在任何特定目录或文件中。 It can even be in a regular .ts file.它甚至可以在一个普通的.ts文件中。 The easiest thing to do is to put the declaration in cache.ts , the same file where you assign mongoose.Query.prototype.cache .最容易做的事情是把在申报cache.ts ,在那里你将相同的文件mongoose.Query.prototype.cache

Edit: I forgot to address your specific question about augmenting a class.编辑:我忘了解决你关于增加课程的具体问题。 As you guessed you can augment a class by using the interface keyword.正如您所猜测的,您可以使用interface关键字来扩充类。 That is because in TypeScript defining a class does two things: it defines a value (the constructor function that is invoked when you call, eg, new DocumentQuery ), and a type for instances of the class which is really an interface.这是因为在 TypeScript 中定义一个类有两件事:它定义一个值(当你调用时调用的构造函数,例如new DocumentQuery ),以及一个真正是接口的类实例的类型。 The value and the type both have the same name.值和类型都具有相同的名称。 Because the type part of a class is an interface you can augment it like any other interface.因为类的类型部分是一个接口,所以您可以像任何其他接口一样对其进行扩充。

So in this case you augment the DocumentQuery type, which is the superclass of Query , but you assign the cache method to the Query prototype.因此,在这种情况下,您增加了DocumentQuery类型,它是Query的超类,但您将cache方法分配给Query原型。 That works because Query extends DocumentQuery , so when you declare that DocumentQuery now has a cache method TypeScript assumes that subclasses, including Query , have the same method.这是有效的,因为Query扩展了DocumentQuery ,因此当您声明DocumentQuery现在具有cache方法时,TypeScript 假定子类,包括Query ,具有相同的方法。 This does lead to a discrepancy: TypeScript now assumes that DocumentQuery instances have a cache method, but you only really defined that method for Query .这确实导致了一个差异:TypeScript 现在假设DocumentQuery实例具有cache方法,但您只是为Query真正定义了该方法。 It would be more accurate to either change your type declaration to augment Query instead of DocumentQuery , or to assign the method implementation to DocumentQuery.prototype instead of to Query.prototype .更改您的类型声明以增加Query而不是DocumentQuery ,或者将方法实现分配给DocumentQuery.prototype而不是Query.prototype会更准确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM