-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Interface that will only allow defined members on derived classes. #14816
Comments
An interface adding a restriction on a how a class is implemented seems strange.. what other languages do that? |
I don't know of any language that would do this I must say. The point would be to instantly resolve any ambiguity on how a member is called. If you think about coordinates, is it If you could define the public members such manner, that could save time for developers by enforcing certain constraints, but still open for extension in form of another interface and closed for unintended modification. |
I think you are looking for something like #2000, and a flag to force all inherited members to be marked as |
Yeah, read the ticket, but that includes indicating |
not sure i understand what you mean.
I think this is the normal case, it is unlikely to write an interface and implement it only once. |
I meant that the flag you described works only if the class is an extension of another. So this solution would work also if you just implement the interface and does not extend a class. Or am I wrong here? |
I would expect TS behavior proposed in #2000, If implemented, would behave the same for interfaces as well as classes. |
I do not think an interface restricting what properties its implementing class can do is a good idea. An interface declares a shape of an API, a class implements that, it is almost always that an implementing class will add additional members, and having that be an error seems wrong. The suggestion in #2000 can be extended to properties. As i said earlier, we felt the value added by doing such checks (with override not being the default) is not worth introducing new concepts to the language. The same logic applies to any language suggestion. a new feature adds complexity both to users and maintainers, and if the value does not offset the cost, then we are all better off without the feature. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Suggestion
Some modifier for strict interfaces. Meaning that implementing this interface will only allow defined members to be declared as public in derived classes.
Extension regards
Pass.
Shall not pass. Should throw error: only
@strict
interfaces may extend@strict
interface Foo.Implementation regards
Pass.
Still pass.
Throw error: boo is not allowed in classes implementing strict class Foo.
The text was updated successfully, but these errors were encountered: