-
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
Suggestion for Strict interfaces #6184
Comments
There's actually not that much variance allowed. You couldn't have a base But there's nothing wrong with Moreover, it's not clear why the definer of |
If it is by design, than that is end of discussion. I just was curious if ther was a reason for it being as it is, which you gave me. I found it strange at first because this isn't allowed in languages like C# and java, where the call signature must match exactly. But as I said, reflecting on how javascript treats extra arguments and return values, it makes sense. |
As an aside, I realized my rhetorical questions at the end there might not have sounded as rhetorical as they were, so apologies for that if it sounded pushy. Thanks! |
No probs, I wanted to answer very strongly on them, but after rereading them I realist the actual questions and tone you tried to set. ps. that wikipedia article is a good read. |
I was shocked when a co-worker informed me of this today. I get that B is a valid substitue for IDispatchable; however, if I am using interfaces as contracts within my codebase, if the contract changes, I would like to be "aware" of it as my implementation may not be robust enough to handle the change. It would be nice if there was a compiler flag (or a interface definition keyword like "strict") available to enforce strict adherence to function signatures in interfaces. |
I encountered a behavior of interface implementations which at first I taught was strange. Consider the following interfaces
Upon implementing this, I expected that typescript would force me to use a matching signature, but it accepts every signature which vaguely matches the implementation.
For example this will not raise an error
If I think about it in terms of JavaScript it makes sense that it would accept B. Even though someone would give me
N
arguments, the function can just ignore them. Moreover even though a function returns anumber
we can still ignore them.But from a OO design perspective this is strange (at least to me). The signature of
B
's methoddispatch
does not even come close to the expected signature.Suggestion
Now this is a long shot, but maybe more people support this idea. What if we can mark a interface (or the interface method) as strict. Which would mean that it would only accept implementations with the exact same function signature.
For example
Another less elegant solution would be a config flag.
I actually don't have an idea if this is within the operation scope of typescript, but sharing ideas is never a bad thing to do. And maybe the makes of typescript have considered this already and made a specific design decisions, in that case I am curious why.
The text was updated successfully, but these errors were encountered: