Repository navigation
fix(merge): replace discriminated union fields when the variant changes - #36
Merged
Merged
Conversation
The runtime merge recursed into a union's typename-keyed shape, which matches no real field, so when a field's variant changed the old variant's fields stayed in place and the new variant's never landed. Treat union fields atomically and replace them wholesale. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
🔥 |
pzuraq
approved these changes
Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The runtime entity merge does not handle a discriminated union field whose variant changes. A union's compiled shape is keyed by
__typename(e.g.{ VariantA: ..., VariantB: ... }), not by field. WhenmergeFieldshit a union field with two plain-object values, it recursed into that typename-keyed shape, which matches none of the actual payload fields. As a result, on a variant change the old variant's object was left untouched (its discriminator and fields persisting) and the new variant's fields never landed.The single-object-union path (
t.union(t.object({...}), t.string)) was unaffected because it collapses to a flat field shape; the bug only surfaced with a true discriminated union of two or more object variants.Fix
Treat a union field as an atomic tagged value and replace it wholesale rather than merging field by field. A changed variant becomes the new value; the old variant's fields can no longer survive.
This mirrors how entity unions already behave: a changed
__typenameyields a different entity key, so the field repoints to a fresh entity. Non-entity unions have no key, so the merge has to reproduce that explicitly. There is no partial-update path to preserve for non-entity unions: a partial variant payload fails validation, so every update carries the full variant.Test
branch-fixes.test.tsadds a regression test: an entity with at.union(QueuedState, RunningState)field, flipped from one variant to the other on refetch, asserting the new discriminator and fields are present and no field from the old variant survives.Full suite green; type-check clean (one unrelated pre-existing error in
react/__tests__/component.test.tsx).