打字稿扩展类以及实现接口

时间:2020-09-07 13:20:34

标签: javascript typescript mongoose interface extends

我正在尝试扩展第三方库类mongooseJs,以便在那里可以使用一些自定义功能。我过去曾经使用Javascript做到过,就像这样

const mongoose = require("mongoose");
class CustomMongoose extends mongoose.Mongoose {
    myCustomMethod() {
        console.log('this is my custom method');
    }
}

const a = new CustomMongoose();
a.model("test"); //This method does get called on parent correctly (code wouldn't work until connection is setup but you get the point)

但是,当我尝试在打字稿中进行类似操作时,如下所示,它给我一个错误

Type 'CustomMongoose' is missing the following properties from type 'CustomMongooseBase': pluralize, connect, createConnection, disconnect, and 39 more.

此代码

const mongoose = require("mongoose");
import { Schema, Model, Document, Mongoose } from "mongoose";

interface CustomMongooseBase extends Mongoose {
  myCustomMethod(): void;
}

class CustomMongoose extends mongoose.Mongoose implements CustomMongooseBase {
    myCustomMethod(): void {
        console.log('this is my custom method');
    }
}

//this is what I would expect to work, if this compiles fine
// const a = new CustomMongoose();
// a.myCustomMethods(); // should work
// a.model("modelName") //should also work, though this is from the base

我想做的是提供自己的接口CustomMongooseBase,这又扩展了Mongoose接口。我希望新类CustomMongoose的调用者具有我的新功能以及所有其他功能。因此,在具体实现中,我扩展了mongoose.Mongoose,它实现了所有方法Mongoose接口所需的实现,而我的自定义类又实现了我自己的接口要求的新方法myCustomMethod的实现,因此这应该可以,但是不能。

我在这里做错了什么? (对于打字稿来说是新手,所以请设法引起注意)

====略有不同的版本,带有不同的错误====

import { Mongoose } from "mongoose";

export interface CustomMongooseBase extends Mongoose //extending at the interface level so that consumers have the full signature of mine as well as parent interface
{ 
  myCustomMethod(): void;
}

class CustomMongoose extends Mongoose implements CustomMongooseBase {
  myCustomMethod(): void {
    console.log("this is my custom method");
  }
}

//this is what I would expect to work, if this compiles fine
// const a = new CustomMongoose();
// a.myCustomMethods(); // should work
// a.model("modelName") //should also work, though this is from the base

这是另外一个错误

Non-abstract class 'CustomMongoose' does not implement inherited abstract member 'ConnectionBase' from class 'typeof import("mongoose")'.ts(2515)

这些是我的package.json依赖项

"dependencies": {
    "@types/mongoose": "^5.7.36",
    "@types/node": "^14.6.4",
    "mongoose": "^5.10.3"
  }

1 个答案:

答案 0 :(得分:0)

我认为问题是

  1. 您同时拥有const mongoose = require("mongoose");import { Schema, Model, Document, Mongoose } from "mongoose";。真的,真的很奇怪。通常,您使用 CommonJS(require ESM(import)。你不要把它们混在一起。

    然后

  2. 您在Mongoose上使用interface CustomMongooseBase extends Mongoose,但在mongoose.Mongoose上使用class CustomMongoose extends mongoose.Mongoose implements CustomMongooseBase。我怀疑mongoose.Mongoose不能解析为同一类型。

我将仅使用一种形式的模块导入并对其命名保持一致来解决该问题:

import { Schema, Model, Document, Mongoose } from "mongoose";

interface CustomMongooseBase extends Mongoose {
  myCustomMethod(): void;
}

class CustomMongoose extends Mongoose implements CustomMongooseBase {
    myCustomMethod(): void {
        console.log('this is my custom method');
    }
}

也就是说,没有任何理由CustomMongooseBase需要扩展Mongoose,您只需删除该位即可:

interface CustomMongooseBase {
  myCustomMethod(): void;
}

class CustomMongoose extends Mongoose implements CustomMongooseBase {
    myCustomMethod(): void {
        console.log('this is my custom method');
    }
}