'Base Class extended Incorrectly
Hey I am using Deno and extending EventTarget class but I don't want the end user to be able to get functions which exist on EventTarget class like addEventListener,dispatchEvent and removeEventListener. I am doing this right now:
class Test extends EventTarget{
constructor(){
super()
}
private addEventListener(){}
private dispatchEvent(){}
private removeEventListener(){}
}
But I am getting Error:
Class 'Test' incorrectly extends base class 'EventTarget'.
Property 'addEventListener' is private in type 'Test' but not in type 'EventTarget'
Solution 1:[1]
...but I don't want the end user to be able to get functions which exist on EventTarget class like
addEventListener,dispatchEventandremoveEventListener...
Then it's not a subclass of EventTarget. A subclass has all the members of its superclass and is an instance of that superclass (see the Liskov Substitution Principle). You should be able to use a subclass instance anywhere a superclass instance is expected.
You'll need to implement a class with the methods you want from EventTarget (perhaps delegated to a private EventTarget instance) rather than subclassing EventTarget.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | T.J. Crowder |
