@@ -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+
124165test ( "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