The TypeSchemaIndex is a comprehensive data structure containing all transformed FHIR definitions in TypeSchema format. It serves as the primary input to code generators and provides utilities for navigating type hierarchies, resolving references, and working with profiles and specializations.
Table of Contents
TypeSchemaIndex is created during the TypeSchema generation phase and serves as the input to all code generators (Writer implementations, Mustache templates, etc.). It organizes all FHIR schemas by canonical URL and package name, enabling efficient lookups and traversals of complex type relationships.
Why TypeSchemaIndex Matters
- Complete Data: Contains ALL types from the FHIR package
- Already Transformed: Types converted from FHIR StructureDefinition to TypeSchema format
- Dependencies Resolved: References between types already established
- Organized: Queryable by type category (resources, complex types, profiles, logical models)
- Relationship-Aware: Includes hierarchy, specialization, and inheritance information
- Read-Only: Frozen snapshot; writes go through Writer methods only
- Queryable: Rich set of utilities for filtering, resolving, and traversing types
Query schemas by type category:
collectComplexTypes(): RegularTypeSchema[]
Returns all complex types (datatypes, backbone elements)
collectResources(): RegularTypeSchema[]
Returns all FHIR resources (Patient, Observation, etc.)
collectLogicalModels(): RegularTypeSchema[]
Returns all logical models
collectProfiles(): ProfileTypeSchema[]
Returns all profiles (constraints on base types)Look up individual schemas by identifier or URL:
resolve(id: Identifier): TypeSchema | undefined
Resolves by fully qualified identifier (identifier object with package, version, name, url, kind)
resolveByUrl(pkgName: PackageName, url: CanonicalUrl): TypeSchema | undefined
Resolves by canonical URL within a specific packageWork with type inheritance and base type relationships:
resourceChildren(id: Identifier): Identifier[]
Returns all resource types that specialize the given resource
tryHierarchy(schema: TypeSchema): TypeSchema[] | undefined
Builds hierarchy chain from schema to base types (safe version, returns undefined if incomplete)
hierarchy(schema: TypeSchema): TypeSchema[]
Builds hierarchy chain from schema to base types (throws if incomplete)Handle type specialization and profile constraints:
findLastSpecialization(schema: TypeSchema): TypeSchema
Finds the most specialized version of a schema
findLastSpecializationByIdentifier(id: Identifier): Identifier
Finds the identifier of the most specialized versionWork with FHIR profiles and their constraints:
flatProfile(schema: ProfileTypeSchema): ProfileTypeSchema
Flattens a profile by resolving all differential constraints into a complete snapshot
isWithMetaField(profile: ProfileTypeSchema): boolean
Checks if a profile includes the meta fieldExport type hierarchy for debugging:
exportTree(filename: string): Promise<void>
Exports the complete type hierarchy tree to a fileconst complexTypes = tsIndex.collectComplexTypes();
const resources = tsIndex.collectResources();
const logicalModels = tsIndex.collectLogicalModels();
const profiles = tsIndex.collectProfiles();const patientIdentifier = { package: "hl7.fhir.r4.core", version: "4.0.1", name: "Patient", url: "http://hl7.org/fhir/Patient", kind: "resource" };
const schema = tsIndex.resolve(patientIdentifier);
const schema2 = tsIndex.resolveByUrl("hl7.fhir.r4.core", "http://hl7.org/fhir/Patient");const children = tsIndex.resourceChildren(patientIdentifier);
const hierarchy = tsIndex.hierarchy(patientSchema);
const safeHierarchy = tsIndex.tryHierarchy(patientSchema);const specialized = tsIndex.findLastSpecialization(patientSchema);
const specializedId = tsIndex.findLastSpecializationByIdentifier(patientIdentifier);const flatProfile = tsIndex.flatProfile(useCorePatientProfile);
const hasMeta = tsIndex.isWithMetaField(profile);await tsIndex.exportTree("./debug/type-tree.txt");Utility functions for processing schemas:
import { groupByPackages, sortAsDeclarationSequence } from "@root/typeschema/utils";
const byPackage = groupByPackages(tsIndex.collectResources());
const sorted = sortAsDeclarationSequence(byPackage["hl7.fhir.r4.core"]);- TypeSchemaIndex & Helper Utilities Implementation:
src/typeschema/utils.ts - TypeSchema Type Definition:
src/typeschema/types.ts - Writer Generator Guide: writer-generator.md
- Mustache Generator Guide: mustache-generator.md
- FHIR Specification: https://www.hl7.org/fhir/