gpt4 book ai didi

javascript - 为什么 TypeScript 会提示父类继承?

转载 作者:行者123 更新时间:2023-12-01 01:07:41 25 4
gpt4 key购买 nike

所以,我有这个代码库:

var MicroEvent  = function(){}
MicroEvent.prototype = {
bind : function(event, fct){
this._events = this._events || {};
this._events[event] = this._events[event] || [];
this._events[event].push(fct);
},
unbind : function(event, fct){
this._events = this._events || {};
if( event in this._events === false ) return;
this._events[event].splice(this._events[event].indexOf(fct), 1);
},
trigger : function(event /* , args... */){
this._events = this._events || {};
if( event in this._events === false ) return;
for(var i = 0; i < this._events[event].length; i++){
this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1))
}
}
};

class PluginManager extends MicroEvent {
registerPlugin(pluginName: string, applicationName: string, active: boolean = true) {
// code
}
// ...code
}

使用此代码我不断收到此错误: error image

我试图添加 .d.ts 文件,如下所示:

declare interface MicroEvent {
registerPlugin(arg1: string, arg2: string, arg3?: boolean): void;
bind(event: string, fct: string):void
unbind(event: string, fct: string):void
trigger(event: string):void
}

但它并没有修复错误。

什么原因导致此错误?

提前致谢!

最佳答案

混合约定(原型(prototype)和类)似乎是自找麻烦(也可能是 TypeScript 的困惑根源)。只坚持一种模式:

class MicroEvent {
bind //...
unbind //...
trigger //...
};

class PluginManager extends MicroEvent { //...

关于javascript - 为什么 TypeScript 会提示父类继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55477125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com