-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Suggestion: Prevent hiding super class properties and methods #9060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
#2000 tracks the override keyword for handling this problem |
I've read through that issue and I feel that my problem is a little different. For me I'd rather be able to designate on my base class that something shouldn't be overridden, maybe #1534 is closer to what I'm thinking, although I'd also like the ability to seal an instance property. |
I do not think this is #2000, this is rather different. this is akin to using a virtual function in the constructor of the base class. usually not a good recipe, see some comments for C++ http://stackoverflow.com/questions/962132/calling-virtual-functions-inside-constructors and for C# http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor. I would suggest either using capture in the call site: constructor()
{
member.a = "Member";
document.body.addEventListener("click", ()=> { this.click() } );
}
} or making this a constructor argument. |
I was thinking of the issue I'm having similar to hiding inherited members in C#
From here: I understand what you mean, however my goal is to prevent the user from redefining the function/property in the subclass. |
still does not solve your issue. the problem is not that you are shadowing a member. the problem is you are using it in the base constructor before the new member have been installed on the object. |
We use a lot of observable objects in our ViewModels so we like to associate a callback for when they are changed in our constructor. It feels right to do this in the constructor, Otherwise I'd have to call some setupEvents() method after the object is constructed. |
then you should capture the document.body.addEventListener("click", ()=> { this.click() } ); instead of: document.body.addEventListener("click", this.click ); |
The only issue I have with this is I wouldn't be able to remove the event listener. |
const listener = ()=> { this.click() };
document.body.addEventListener("click", listener);
document.body.removeEventListener("click", listener); |
Code
It would be nice if we were able to generate a warning or error to say that B's member is hiding A's member, as well as the fact that B's click is hiding A's click. I'm not sure whether a new keyword would be appropriate, or just an option in tsconfig.json to generate an error or warning would make me happy.
Not having this has lead me to some head scratching to why things aren't working as I rearrange how things inherit from each other due to me not noticing that I have redefined a method or property in the subclass.
The text was updated successfully, but these errors were encountered: