TS: make Bundle<T> generic by propagating nested type params#148
Open
TS: make Bundle<T> generic by propagating nested type params#148
Conversation
Parent interfaces now become generic when any of their fields reference
a generic nested type, and the field reference carries the type
parameter through.
Before: `Bundle { entry?: BundleEntry[] }` (always BundleEntry<Resource>)
After: `Bundle<T extends Resource = Resource> { entry?: BundleEntry<T>[] }`
Default `= Resource` keeps existing call sites working. Callers can now
narrow, e.g. `Bundle<Patient | Observation>`.
Refactored the generic-params computation in generateType() into a
reusable helper (computeGenericInfo) and threaded per-nested-type info
from generateNestedTypes() back into the parent generation.
- Bundle.ts now declares `Bundle<T extends Resource = Resource>` with `entry?: BundleEntry<T>[]`. - Added demo tests showing discriminated-union narrowing via `Bundle<Patient | Observation>` and backwards-compat default.
3868aac to
a9cee6f
Compare
Drop the standalone computeGenericInfo helper and the GenericInfo type in favor of inlining the (small) logic directly into generateType. The helper duplicated the existing typeFamilyFields detection that already worked; the genuinely new bit is just the nested-type pass-through. generateType now returns the parent's GenericParam[] (only paramList is needed by callers — fieldMap and nestedArgsByField are local concerns), and generateNestedTypes threads a Record<string, GenericParam[]> back to the parent. Net change vs main: +61/-32 (was +81/-36). No behavior change.
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.
Closes #145.
Summary
Bundle(viaBundleEntry<T>) and will also kick in for any other resource whose nested types happen to be generic.Generated output (before / after)
Motivation: callers couldn't narrow
Bundle.entry[i].resourcewithout hand-rolled intersection types.Before:
After:
Usage
Implementation
generateType()into a reusable helpercomputeGenericInfothat works for both nested and top-level schemas.generateNestedTypesnow returns a per-nested-typeRecord<string, GenericInfo>which is threaded back into the parent'sgenerateTypecall.BundleEntry<T>[]).Tests
generates Bundle with generic entry type (nested generic propagation)intypescript.test.ts— directly covers the nested propagation path.resource.test.tsdemo to drop the explicit(r): r is Observationpredicate; TS 5.5+ infers it automatically from the discriminated union, which is the whole point of the feature.