Improve EPC for unions with multiple discriminants#32711
Improve EPC for unions with multiple discriminants#32711jack-williams wants to merge 1 commit intomicrosoft:masterfrom
Conversation
051c748 to
a67c3cc
Compare
|
A: yes. |
src/compiler/checker.ts
Outdated
| } | ||
| if (candidates) { | ||
| let matchID: string | false | undefined; | ||
| candidates.forEach((value, id) => { |
There was a problem hiding this comment.
A comment explaining what criteria this is filtering/selecting on would be useful. I think it's that it returns the first type which matches all discriminant properties (out of multiple which may match), but some text expressing that would be nice.
There was a problem hiding this comment.
Will do. If there are multiple valid matches it fails rather than picking the first. I did this to avoid situations where the order of the union influenced whether you got an error, sometimes incorrectly. (I think it was a case where some constituents were technically subtypes of another, but the union was unreduced).
There was a problem hiding this comment.
@weswigham Done. In the case where there are multiple candidates we could return a new union of just those candidates, but I'm not sure of the error message and perf consequences---if you want me to experiment with that approach let me know.
Thanks. What I've written feels kind of messy and could be cleaned up, but I hope the algorithm is on the right lines. |
a67c3cc to
93de3ef
Compare
|
Sooooo, I also fixed this bug yesterday, in a different PR, while working on a different bug. The algorithms are almost the same, but I feel like my solution is easier to read:
@jack-williams would you mind taking a look? I'll merge your commit in order to get your test and make sure you get attribution for this fix as well. (Thanks @weswigham for pointing this out to me.) |
Fixes #32657
Q: Do I need to explicitly coerce map keys to string like:
candidates.set("" + type.id, 1);?