Skip to content

TS: Generic BundleEntry<T> and DomainResource<T> for resource-typed fields#109

Merged
ryukzak merged 3 commits intomainfrom
ts-type-family-generics
Mar 18, 2026
Merged

TS: Generic BundleEntry<T> and DomainResource<T> for resource-typed fields#109
ryukzak merged 3 commits intomainfrom
ts-type-family-generics

Conversation

@ryukzak
Copy link
Copy Markdown
Collaborator

@ryukzak ryukzak commented Mar 18, 2026

Depends on #110.

Types like BundleEntry have fields typed as a resource base type (Resource, DomainResource) that acts as a type family with many children. This change detects such fields and emits generic parameters so consumers can narrow the type.

  • writer.ts: scan fields for resource type families (via isResourceIdentifier + resourceChildren().length > 0), build <T extends Resource = Resource> generic declarations
  • utils.ts: add genericFieldMap param to resolveFieldTsType() for generic field substitution
  • profile-slices.ts: add extractResourceTypeFromMatch() helper; compute typedBaseType on SliceDef (e.g. BundleEntry<Patient>); use it in setters, getters, and flat type aliases
  • profile.ts: import matched resource types for type discriminator slices

Before:

export interface BundleEntry extends BackboneElement {
    resource?: Resource;
}

export type ExampleTypedBundle_Entry_PatientEntrySliceFlat = BundleEntry;

public setPatientEntry(input?: ... | BundleEntry): this
public getPatientEntry(mode: 'raw'): BundleEntry | undefined

After:

export interface BundleEntry<T extends Resource = Resource> extends BackboneElement {
    resource?: T;
}

export type ExampleTypedBundle_Entry_PatientEntrySliceFlat = BundleEntry<Patient>;

public setPatientEntry(input?: ... | BundleEntry<Patient>): this
public getPatientEntry(mode: 'raw'): BundleEntry<Patient> | undefined

Affected types: BundleEntry<T>, BundleEntryResponse<T>, DomainResource<T>. Default parameter = Resource means unparameterized usage is unchanged.

Tests & examples

  • Add compile-time type narrowing tests: access Patient/Organization-specific fields without casts, DomainResource<Patient> contained narrowing, @ts-expect-error for wrong resource type
  • Use explicit BundleEntry<Patient> / BundleEntry<Organization> in set/get tests
  • Add Patient and Organization to typed bundle test tree shake
  • Update snapshots for parameterized setters/getters
  • Regenerate R4 and US Core examples with generic type parameters

@ryukzak ryukzak changed the title TypeSchema/TS: Add generic type parameters for type-family fields TS: Add generic type parameters for type-family fields Mar 18, 2026
@ryukzak ryukzak changed the base branch from main to narrow-discriminator-type March 18, 2026 16:20
@ryukzak ryukzak force-pushed the narrow-discriminator-type branch from 1e68306 to a2f0afc Compare March 18, 2026 16:26
@ryukzak ryukzak force-pushed the ts-type-family-generics branch from 9aa6d19 to b52d29e Compare March 18, 2026 16:27
@ryukzak ryukzak force-pushed the narrow-discriminator-type branch from a2f0afc to 6537262 Compare March 18, 2026 16:32
…rator

Detect fields typed as resource type families (Resource, DomainResource)
and emit generic parameters so consumers can narrow the type, e.g.
BundleEntry<Patient> gives resource?: Patient.
@ryukzak ryukzak force-pushed the ts-type-family-generics branch from b52d29e to ed8cb23 Compare March 18, 2026 16:36
Base automatically changed from narrow-discriminator-type to main March 18, 2026 16:36
@ryukzak ryukzak force-pushed the ts-type-family-generics branch from b2a7483 to 69ecfe1 Compare March 18, 2026 17:28
@ryukzak ryukzak changed the title TS: Add generic type parameters for type-family fields TS: Generic BundleEntry<T> and DomainResource<T> for resource-typed fields Mar 18, 2026
@ryukzak ryukzak merged commit b56abfe into main Mar 18, 2026
31 checks passed
@ryukzak ryukzak deleted the ts-type-family-generics branch March 18, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant