打字稿扩展模块(扩展) [英] Typescript extend module (augmentation)
问题描述
我想用自己的类和函数扩展已安装的模块(猫鼬). 我写了函数和类,它们工作正常.
i want to extend a installed module (Mongoose) with my own class and function. I wrote the function and class and they work fine.
现在我想将它们添加到mongoose
模块中.
所以我现在拥有的是这个文件:
mongoInstance.ts
Now i wanted to add them to the mongoose
module.
So what i have now is this file:
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
}
现在我可以在我的应用中的任何地方做
Now anywhere in my app i can do:
otherFile.ts
import * as mg from 'mongoose'
//doesnt work
var test1= new mg._Schema({})
//works
var test2= new mg.Schema({})
因此VSCode向我推荐了我的课程,并且IntellIsense起作用了.但是似乎没有该类的实现.
Webpack在将代码编译为bundle.js
时不会引发任何错误,但是当我尝试使用node bundle.js
运行bundle.js时,它说: TypeError:mg._Schema不是构造函数 >
So VSCode suggests me my class, and IntellIsense works. But it seems there is no implementation of the class.
Webpack doesn´t throw any errors when compiling my code to bundle.js
but when i try to run my bundle.js with node bundle.js
it says: TypeError: mg._Schema is not a constructor
推荐答案
您将需要两件事:
- 用声明文件扩展
mongoose
的类型定义. - 添加功能,以便模块匹配其新定义.
- Extend the type definition of
mongoose
with a declaration file. - Add functionality so the module matches its new definition.
mongoose.d.ts
mongoose.d.ts
创建一个声明文件(*.d.ts
)并将其包含在您的项目中.
Create a declaration file (*.d.ts
) and include it in your project.
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;
}
mongoose-instance.ts
mongoose-instance.ts
添加您刚刚声明的功能.
Add the functionality you just declared.
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
consumer.ts
从现在开始,使用本地版本的mongoose
.
From now on, use your local version of mongoose
.
import mongoose from '../path/to/mongoose-instance';
console.log(mongoose._Schema);
如果遇到与默认导入有关的任何问题,请确保在tsconfig.json
中启用以下两个标志:allowSyntheticDefaultImports
和esModuleInterop
.
If you encounter any problems related to default imports, make sure to enable these two flags in your tsconfig.json
: allowSyntheticDefaultImports
and esModuleInterop
.
这篇关于打字稿扩展模块(扩展)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!