fix(db): preserve union types in select result extraction#1512
Open
nathan-muir wants to merge 1 commit intoTanStack:mainfrom
Open
fix(db): preserve union types in select result extraction#1512nathan-muir wants to merge 1 commit intoTanStack:mainfrom
nathan-muir wants to merge 1 commit intoTanStack:mainfrom
Conversation
d56337e to
3436682
Compare
When a collection field is a union of object types (e.g.,
`{ type: 'pdf'; url: string } | { type: 'image'; width: number }`),
selecting that field via `.select()` collapses the union into a single
type containing only the intersection of keys. This happens because
`Ref<T>` uses a mapped type `[K in keyof T]`, and when `T[K]` is a
union of objects, `IsPlainObject` returns true (it distributes), causing
`Ref` to recurse into the union. But `keyof (A | B | C)` only yields
common keys, destroying the discriminated union.
3436682 to
ae0abd1
Compare
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.
Fixes #1511
🎯 Changes
When a collection field is a discriminated union of object types, selecting that field via
.select()collapses the union into a single type with only the intersection of keys.The root cause is in
ResultTypeFromSelect: when aRef<T>holds a union type,ExtractRefrecurses into the mapped keys viakeyof/Prettify, which collapse the union to only common keys.The fix detects when a
Ref's branded type (RefLeaf<T>) is a union and extracts it directly, bypassingExtractRef. This preservesRef<T>'s mapped type structure so that common keys of the union remain accessible in.where()and.orderBy()callbacks (e.g.,eq(i.document.type, 'pdf')).New helpers:
IsUnion<T>— detects union typesExtractRefBrand<T>— extracts the raw type from aRefLeaf's brandTests added:
✅ Checklist
pnpm test.🚀 Release Impact