Skip to content

TS: accept all FHIR literal reference forms in Reference<T>#144

Merged
ryukzak merged 3 commits intomainfrom
fix/reference-literal-forms
Apr 21, 2026
Merged

TS: accept all FHIR literal reference forms in Reference<T>#144
ryukzak merged 3 commits intomainfrom
fix/reference-literal-forms

Conversation

@ryukzak
Copy link
Copy Markdown
Collaborator

@ryukzak ryukzak commented Apr 21, 2026

Closes #141.

Changes

  • Widen the generated Reference<T>.reference field from `${T}/${string}` to a union that covers every form allowed by FHIR references: relative, absolute http/https, urn:uuid, urn:oid, and #fragment.
  • Add a demo in examples/typescript-r4/resource.test.ts that assigns each literal form to a typed Reference field, so the widened union is exercised by tsc.
  • Regenerate src/fhir-types and the typescript-r4 / typescript-us-core example fhir-types.

Before / after

Motivation: Reference<"Patient"> was rejecting urn:uuid placeholders used in transaction Bundles, forcing an inline cast that lies about the string's shape.

Before:

export interface Reference<T extends string = string> extends Element {
    reference?: `${T}/${string}`;
}

const patientUrn = `urn:uuid:${randomUUID()}`;
const bp = USCoreBloodPressureProfile.create({
    status: "final",
    // ❌ TS2322: Type 'string' is not assignable to type `Patient/${string}`
    subject: { reference: patientUrn },
});

After:

export interface Reference<T extends string = string> extends Element {
    reference?:
        | `${T}/${string}`
        | `http://${string}`
        | `https://${string}`
        | `urn:uuid:${string}`
        | `urn:oid:${string}`
        | `#${string}`;
}

// compiles — no cast required
const bp = USCoreBloodPressureProfile.create({
    status: "final",
    subject: { reference: patientUrn },
});

ryukzak added 3 commits April 21, 2026 16:56
Fixes #141. The generated Reference<T> type narrowed reference to `${T}/${string}`, which rejected other forms allowed by the FHIR spec (urn:uuid, urn:oid, absolute URLs, fragments).

The field type is now a union covering: relative `${T}/${string}`, absolute `http://…` / `https://…`, Bundle placeholder `urn:uuid:…`, OID `urn:oid:…`, and contained fragment `#…`.
Add a demo in examples/typescript-r4/resource.test.ts that assigns each of the six allowed literal forms (relative, urn:uuid, urn:oid, http, https, fragment) to a typed Reference field, so the widened template-literal union is exercised by tsc.
Regenerate src/fhir-types and the typescript-r4/typescript-us-core example fhir-types to reflect the widened Reference.reference union.
@ryukzak ryukzak merged commit cc8f05b into main Apr 21, 2026
37 checks passed
@ryukzak ryukzak deleted the fix/reference-literal-forms branch April 21, 2026 15:19
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.

Reference.reference type rejects valid FHIR reference forms (urn:uuid, absolute URLs, fragments)

1 participant