I tried the following way to create an interface and implement it.
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
implemented that in a class by extending it. (Note that I have used ts-mixer to have multiple inheritance.
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
This will throw the error class should implement a methodA() method. Which means the check I do in the interface fails if (!this.methodA).
This works fine when I remove the Mixin and extend only the interface. (class ClassA extends AInterface)
Is there a better way to do this or how can I fix this?
Node version - 14