【发布时间】:2022-01-23 14:32:31
【问题描述】:
我尝试了以下方法来创建接口并实现它。
class AInterface {
constructor () {
if (!this.methodA) {
throw new Error('class should implement a methodA() method')
} else if (!this.methodB) {
throw new Error('class should implement a methodB() method')
}
}
}
export default AInterface
通过扩展一个类来实现它。 (请注意,我使用 ts-mixer 进行多重继承。
import AInterface from './AInterface'
import { Mixin } from 'ts-mixer'
class ClassA extends Mixin(AnotherClass, AInterface) {
constructor () {
super()
}
methodA () {
return 'test'
}
methodB () {
return 'test'
}
}
export default ClassA
这将引发错误class should implement a methodA() method。这意味着我在界面中所做的检查失败if (!this.methodA)。
当我删除 Mixin 并仅扩展接口时,这可以正常工作。 (class ClassA extends AInterface)
有没有更好的方法来做到这一点或者我该如何解决这个问题?
节点版本 - 14
【问题讨论】:
标签: javascript node.js interface multiple-inheritance ts-mixer