-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Extend rules for merging specialized call signatures in case of interface ... extends A, B #2888
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
For some background on why this behavior exists: A set of function overloads is actually a single property of a type with multiple call signatures. So SubA's type actually looks like this: interface SubA {
get: { (property: "name"): string; (property: string): any }
} SubB has a similar type with a single property named In contrast, when you merge an interface type the compiler collects all the declarations for that interface type and then processes the resulting set of members. So the compiler doesn't instantiate a While I understand the inconsistency here feels a bit odd I'm not sure it's something that's going to change. |
Tanks for the explanation, @danquirk!
and
The merging algorithm suggested above still seems applicable to me.
Could you give an example, where this kind of merging algorithm would break the typing concept or lead to further inconsistencies? |
The algorithm you describe is already the merging algorithm for bare call signatures on interfaces. The intended design is that if you want to merge call signatures, you do so on an interface type that just has call signatures. If you mix in call signatures through a property, it's less clear that you intend those to merge, especially since there's no obvious runtime parallel to that behavior. |
If you mix in call signatures through a property, it's less clear that you intend those to merge This is the usability portion of the problem. There're a lot of places you could imagine allowing merging or overwriting existing values/properties but we have to balance between what JS patterns are handy vs how often that behavior was actually an unintended mistake (or what the implicit intended behavior is). especially since there's no obvious runtime parallel to that behavior. And this is the crux of the technical issue as far as why these function typed properties don't merge. If this was really blocking how you implement certain JS patterns we'd consider alternative options. But as it stands it's creating some pain here but also for a good reason in the cases where you didn't intend to have this collision/merge. |
Extend the rules for merging type members in case of extends as follows:
Merge interfaces like they had the same name. Afterwards, go through the merged members list from the last to the first. Remove all call signature declarations for which a type-equivalent one did already occur in the list.
At least in simple cases, like in the example below, this should yield the expected result.
Maybe there are some edge cases that still need to yield an error or that need the mentioned workaround.
Example:
Relates to #2871
The text was updated successfully, but these errors were encountered: