const MyInterface = new Interface('myMethodA', 'myMethodB')
Next, just make your class extend from the interface. Make sure you call super() within your class's constructor.
class MyClass extends MyInterface { constructor () { super() } myMethodA () { // ...implementation goes here }}
Now, whenever you try to instantiate MyClass, the interface will be enforced.
const instance = new MyClass()// throws a new error with the message:// 'The following function(s) need to be implemented for class MyClass: myMethodB'
Of course, the interface is enforced on all subclasses as well.
class MySubClass extends MyClass { constructor () { super() } myMethodA () { // override 'myMethodA' }}const instance = new MySubClass()// still throws an error with the message:// 'The following function(s) need to be implemented for class MyClass: myMethodB'
Interfaces can be enforced for classes defined the old way too.