Skip to content

TS: static is() type guard on profile classes#147

Merged
ryukzak merged 2 commits intomainfrom
feat/profile-is-type-guard
Apr 22, 2026
Merged

TS: static is() type guard on profile classes#147
ryukzak merged 2 commits intomainfrom
feat/profile-is-type-guard

Conversation

@ryukzak
Copy link
Copy Markdown
Collaborator

@ryukzak ryukzak commented Apr 22, 2026

Closes #146.

Summary

  • Generate a static is(resource: unknown): resource is BaseType on profile classes for ergonomic collection filtering without hand-rolling resourceType predicates or relying on from() throwing.
  • Emitted only for profiles that can be meaningfully discriminated:
    • resource profiles (with meta) — check resourceType + meta.profile.includes(canonicalUrl)
    • extension profiles — check url === canonicalUrl
  • Skipped otherwise, since a check of only resourceType (or always-true) would be a misleading guard.

Usage

The motivating example from the issue collapses to:

const bps = (bundle.entry ?? [])
  .map(e => e.resource)
  .filter(USCoreBloodPressureProfile.is) // narrows to Observation matching this profile
  .map(o => USCoreBloodPressureProfile.apply(o));

Generated output (resource profile)

static is (resource: unknown) : resource is Observation {
    if (typeof resource !== "object" || resource === null) return false;
    const r = resource as { resourceType?: string; meta?: { profile?: string[] } };
    if (r.resourceType !== "Observation") return false;
    return (r.meta?.profile ?? []).includes(observation_bpProfile.canonicalUrl);
}

Generated output (extension profile)

static is (resource: unknown) : resource is Extension {
    if (typeof resource !== "object" || resource === null) return false;
    return (resource as { url?: string }).url === birthPlaceProfile.canonicalUrl;
}

ryukzak added 2 commits April 22, 2026 14:01
Adds a cheap, non-throwing type guard to generated profile classes so
callers can filter collections without hand-rolling resourceType
predicates or relying on from() throwing.

Emitted only when the profile can be meaningfully discriminated:
- resource profiles with a meta field — check resourceType + meta.profile
- extension profiles — check url === canonicalUrl

Skipped otherwise, since a check of only resourceType (or always-true)
would be a misleading guard.
- Regenerated profile classes for R4 examples (Observation profiles
  and Extension profiles) pick up the new is() method.
- Added demo + regression tests for is() showing the filter pattern
  from issue #146.
@ryukzak ryukzak merged commit 3693101 into main Apr 22, 2026
37 checks passed
@ryukzak ryukzak deleted the feat/profile-is-type-guard branch April 22, 2026 13:41
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.

Feature: non-throwing profile check / type guard for filtering collections

1 participant