Skip to content

Commit 2ee3308

Browse files
committed
TS: regenerate Bundle and add Bundle<T> demo tests
- 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.
1 parent a41868d commit 2ee3308

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

examples/typescript-r4/fhir-types/hl7-fhir-r4-core/Bundle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ export interface BundleLink extends BackboneElement {
4949
}
5050

5151
// CanonicalURL: http://hl7.org/fhir/StructureDefinition/Bundle (pkg: hl7.fhir.r4.core#4.0.1)
52-
export interface Bundle extends Resource {
52+
export interface Bundle<T extends Resource = Resource> extends Resource {
5353
resourceType: "Bundle";
5454

55-
entry?: BundleEntry[];
55+
entry?: BundleEntry<T>[];
5656
identifier?: Identifier;
5757
link?: BundleLink[];
5858
signature?: Signature;

examples/typescript-r4/resource.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,47 @@ test("Bundle with resources", () => {
121121
expect(bundle).toMatchSnapshot();
122122
});
123123

124+
test("Bundle<T> narrows entry resources without type predicates", () => {
125+
// A bundle carrying only Patients and Observations
126+
const patient = createPatient();
127+
assert(patient.id);
128+
const observation = createObservation(patient.id);
129+
const bundle: Bundle<Patient | Observation> = {
130+
resourceType: "Bundle",
131+
type: "transaction",
132+
entry: [
133+
{ fullUrl: `urn:uuid:${patient.id}`, resource: patient },
134+
{ fullUrl: `urn:uuid:${observation.id}`, resource: observation },
135+
],
136+
};
137+
138+
// Discriminated-union narrowing works without a `r is Observation` predicate
139+
const observations: Observation[] = (bundle.entry ?? [])
140+
.map((e) => e.resource)
141+
.filter((r): r is Observation => r?.resourceType === "Observation");
142+
143+
expect(observations).toHaveLength(1);
144+
expect(observations[0]!.id).toBe("glucose-obs-1");
145+
});
146+
147+
test("Bundle<T> entry type is BundleEntry<T>", () => {
148+
const patient = createPatient();
149+
const entry: BundleEntry<Patient> = { fullUrl: `urn:uuid:${patient.id}`, resource: patient };
150+
// resource is narrowed to Patient, not Resource
151+
expect(entry.resource?.resourceType).toBe("Patient");
152+
});
153+
154+
test("Bundle defaults to Bundle<Resource> (backwards compatible)", () => {
155+
const patient = createPatient();
156+
// No type param — entry.resource is Resource | undefined (original behaviour)
157+
const bundle: Bundle = {
158+
resourceType: "Bundle",
159+
type: "collection",
160+
entry: [{ fullUrl: `urn:uuid:${patient.id}`, resource: patient }],
161+
};
162+
expect(bundle.entry).toHaveLength(1);
163+
});
164+
124165
test("Reference accepts all FHIR literal reference forms", () => {
125166
// Relative reference — still narrowed to the typed form
126167
const relative: Observation["subject"] = { reference: "Patient/123" };

0 commit comments

Comments
 (0)