Repository navigation
feat(orm): implement discriminated union return types for delegate (polymorphic) models - #2
Merged
Merged
Conversation
…hic) models Agent-Logs-Url: https://github.com/motopods/zenstack/sessions/678a87a1-1e67-44f9-95ee-e77f3d5e81cc Co-authored-by: motopods <58200641+motopods@users.noreply.github.com>
Agent-Logs-Url: https://github.com/motopods/zenstack/sessions/678a87a1-1e67-44f9-95ee-e77f3d5e81cc Co-authored-by: motopods <58200641+motopods@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
motopods
April 2, 2026 05:04
View session
motopods
approved these changes
Apr 2, 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.
Summary
Querying a delegate (polymorphic base) model such as
db.asset.findMany()now returns a proper TypeScript discriminated union instead of the flat base type.Before:
findFirstOrThrow()returnedAsset— accessingr.durationwas always a type error, and narrowing byr.assetTypehad no effect on the type.After:
findFirstOrThrow()returns:TypeScript can now narrow the result via the discriminator field:
Multi-level delegation (Asset → Video → RatedVideo) is fully expanded so every concrete leaf model appears in the union. The
selectpath continues to return a flat result as before (explicitly selected fields remove polymorphic information).Changes
packages/orm/src/client/crud-types.tsIsDelegateModel,GetModelDiscriminator,GetSubModelsto schema imports.FlatModelResulthelper type — the original scalar-field object expression extracted into a named alias for the leaf case.DelegateUnionResult<Schema, Model, Options, SubModel, Omit, _Depth>:DefaultModelResultto useDelegateUnionResultfor delegate models._IsGenericModeldefault type parameter (checks[string] extends [Model]) that short-circuits the entire delegate expansion when TypeScript is evaluatingClientContract<SchemaDef>(the generic base interface). This keeps the type instantiation count bounded (~1.9M vs 1.6M baseline, well below TypeScript's recursion limit).tests/e2e/orm/schemas/delegate/typecheck.tsfindFirstOrThrowand theinclude: { assets: true }relation path).