Allow cyclic unions to discriminate on nested paths#1553
Conversation
|
Any feedback on this @ssalbdivad? (Happy holidays, no pressure! 🦃) |
|
@ssalbdivad I'm eager to get this in. Any notes I can take action on to get this PR ready? |
|
Hey @shaungrady very sorry for the delay getting back and thanks for working on this! I'm going to try and take a look this weekend. Honestly I've been avoiding this a bit because of the complexity of some of the issues to do with recursive discriminated unions in the past which is why I disabled them for cyclic types. I'll try to give this another once over myself, but could you take a look at closed issues that mention bugs with recursive discriminated unions and make sure they are all reflected in our unit tests and still function correctly with this change? If so, I'm very grateful for your work in such a complex area of the code base and am excited to do a final review and ship it |
|
No sweat, I'll search through closed issues to see what I can see. |
|
Great, thanks so much! I'm guessing many of those cases are already reflected in the tests so no need to duplicate if you can search the code base and find them, but any that aren't would be a great addition. |
84ee491 to
80e0593
Compare
|
Full disclosure, I used I rebased/pushed. Let me know if there's anything else you need after you get a chance to review it more closely. AnalysisRelevant
Unrelated
|
|
So it looks like for whatever reason unrelated to the change disallowing shallow discrimination for cyclic types, something I've done since #1425 when cyclic discriminated unions were broken actually fixed them. I'm still not totally sure what, but all the issues cited there as a reason to disable them (#1284, #1209, #1367, #1188) have now been added to tests and are working. I've adjusted this PR to add tests for these which are now passing, and removed the constraint around shallow discrimination which seems to not be needed. There are some cases where undiscriminated unions actually give a clearer error message, but that should be handled more generally as part of some of the other issues in the backlog. Thanks for taking a look at this and I'll be merging this PR as soon as CI passes ❤️ |
|
Appreciate your help with this @ssalbdivad! |
|
Would you be willing to cut a release for this soon? |
Fix cyclic union discrimination
Fixes #1547
Problem
Discriminated unions containing circular references were completely excluded from discrimination, causing validation to check all branches and produce confusing error messages referencing irrelevant branches.
Solution
Allow cyclic unions to use property-based discrimination (e.g., a
typediscriminator field) while blocking structural discrimination that would attempt to describe the entire recursive structure.The fix filters discrimination candidates to only include those with
path.length > 0, ensuring cyclic unions discriminate on specific properties rather than overall object structure.Changes
discriminate()path.length === 0) for cyclic unionsExample
mainbranchpnpm prCheckslocally