Node.js 类型脚本扩展模块(扩充)

Node.js 类型脚本扩展模块(扩充),node.js,typescript,mongoose,Node.js,Typescript,Mongoose,我想用我自己的类和函数扩展一个已安装的模块(Mongoose)。 我编写了函数和类,它们工作得很好 现在我想把它们添加到mongoose模块中。 所以我现在得到的是这个文件: mongoInstance.ts import * as mg from 'mongoose' class _Schema extends mg.Schema { constructor(definition?: mg.SchemaDefinition, options?: mg.SchemaOptions) {

我想用我自己的类和函数扩展一个已安装的模块(Mongoose)。 我编写了函数和类,它们工作得很好

现在我想把它们添加到
mongoose
模块中。 所以我现在得到的是这个文件: mongoInstance.ts

import * as mg from 'mongoose'

class _Schema extends mg.Schema {
    constructor(definition?: mg.SchemaDefinition, options?: mg.SchemaOptions) {
        super(definition, options)
        this.addData()
    }
    private addData() {
        //do stuff
    }
}  

function _limitedRequest(schema: mg.Schema, options = 200) {
    schema.pre("find", function (next) {
        this.limit(options)
        next()
    })
}

declare module 'mongoose' {
    export class _Schema extends Schema {
        constructor(definition?: SchemaDefinition, options?: SchemaOptions)
        private addData(): void
    }
    export function _limitedRequest(schema: Schema, options?: number): void
}
import * as mg from 'mongoose'
//doesnt work
var test1= new mg._Schema({})
//works
var test2= new mg.Schema({})
现在,我可以在我的应用程序中的任何位置执行以下操作:

otherFile.ts

import * as mg from 'mongoose'

class _Schema extends mg.Schema {
    constructor(definition?: mg.SchemaDefinition, options?: mg.SchemaOptions) {
        super(definition, options)
        this.addData()
    }
    private addData() {
        //do stuff
    }
}  

function _limitedRequest(schema: mg.Schema, options = 200) {
    schema.pre("find", function (next) {
        this.limit(options)
        next()
    })
}

declare module 'mongoose' {
    export class _Schema extends Schema {
        constructor(definition?: SchemaDefinition, options?: SchemaOptions)
        private addData(): void
    }
    export function _limitedRequest(schema: Schema, options?: number): void
}
import * as mg from 'mongoose'
//doesnt work
var test1= new mg._Schema({})
//works
var test2= new mg.Schema({})
所以VSCode建议我学习我的课程,IntellIsense也可以。但似乎没有实现这个类。
当我将代码编译到
bundle.js
时,Webpack不会抛出任何错误,但当我尝试使用
node bundle.js运行bundle.js
时,它会显示:TypeError:mg.\u Schema不是构造函数

在mongoInstance.ts中这样导入它:

import mongoose from "mongoose";
mongoInstance.ts中的导出类

export class _Schema extends mg.Schema { ...
在你的otherFile.ts中

import { _Schema} from "./otherFile";

您将需要两件事:

  • 使用声明文件扩展
    mongoose
    的类型定义
  • 添加功能,使模块与其新定义匹配
  • mongoose.d.ts

    创建一个声明文件(
    *.d.ts
    )并将其包含在项目中

    import * as mg from 'mongoose';
    
    declare module 'mongoose' {
      export class _Schema extends mg.Schema {
        constructor(definition?: mg.SchemaDefinition, options?: mg.SchemaOptions);
      }
    
      export function _limitedRequest(schema: mg.Schema, options: number): void;
    }
    
    猫鼬实例.ts

    添加刚才声明的功能

    import mg from 'mongoose';
    
    mg._Schema = class extends mg.Schema {
        constructor(definition?: mg.SchemaDefinition, options?: mg.SchemaOptions) {
            super(definition, options)
            this.addData()
        }
        private addData() {
            //do stuff
        }
    }
    
    mg._limitedRequest = function _limitedRequest(schema: mg.Schema, options = 200) {
      schema.pre("find", function (next) {
          this.limit(options)
          next()
      })
    }
    
    export default mg;
    
    consumer.ts

    从现在起,使用本地版本的
    mongoose

    import mongoose from '../path/to/mongoose-instance';
    
    console.log(mongoose._Schema);
    

    如果遇到与默认导入相关的任何问题,请确保在
    tsconfig.json
    中启用这两个标志:
    allowSyntheticDefaultImports
    esModuleInterop

    嘿,感谢您的支持。但这不起作用。我不能只导入猫鼬,因为猫鼬没有默认的导出。如果我将import station添加到otherFile.ts,也不会更改错误消息。(我不想显式地导入我的_模式,我认为这适用于Mongoose模块增强导入Mongoose这样应该可以。如果你想在otherFile.ts中导入它,请确保在mongoInstance.ts中导出类。我建议不要修补现有模块,因为其他开发人员很难理解它是什么。)你正在这样做。好吧,我不理解。但是当我只是导出我的函数和_Schema类时,我经常会发现自己导入mongoose和Schema,这意味着当复杂性增加时会有很多导入语句。有没有一种方法不显式导入_Schema?是的,这段代码很有效。问题是,
    mg._Schema=class扩展了Schema
    给我警告,因为
    \u Schema
    不是mongoose的属性这意味着我的答案的第一部分没有正确实现-它的作用是告诉TypeScript Schema是mongoose的属性。你确定
    mongoose.d.ts
    包含在你的项目中吗?请查看
    文件
    /
    include
    /
    tsconfig.json
    中排除
    。现在我执行了
    声明模块“mongoose”
    在mongoInstance.ts中。它是这样工作的。但是当我尝试分配
    mg时,它仍然会给出一个错误。_Schema=class extensed Schema
    它说我不能分配它,因为它是一个只读值。你是否将它作为
    从“mongoose”导入mg
    从“mongoose”导入mg*;
    ?它只能使用第一种方法。我不知道将代码修改为在没有默认导入/导出的情况下工作。因此,在mongoInstance.ts中,我执行
    export{mg}