@@ -15,6 +15,7 @@ import type {
1515 FieldSlice ,
1616 FieldSlicing ,
1717 Name ,
18+ PackageMeta ,
1819 RegularField ,
1920 RichFHIRSchema ,
2021 TypeIdentifier ,
@@ -25,6 +26,35 @@ import { mkBindingIdentifier, mkIdentifier } from "./identifier";
2526import { mkSliceNameCandidates } from "./name-candidates" ;
2627import { mkNestedIdentifier } from "./nested-types" ;
2728
29+ const R5_ONLY_TYPES = new Set ( [
30+ "Availability" ,
31+ "CodeableReference" ,
32+ "ExtendedContactDetail" ,
33+ "MonetaryComponent" ,
34+ "RatioRange" ,
35+ "VirtualServiceDetail" ,
36+ ] ) ;
37+
38+ const dependsOnR4Core = ( register : Register , pkg : PackageMeta ) : boolean => {
39+ const pkgIndex = register . resolver [ packageMetaToFhir ( pkg ) ] ;
40+ if ( ! pkgIndex ) return false ;
41+ for ( const options of Object . values ( pkgIndex . canonicalResolution ) ) {
42+ for ( const opt of options ) {
43+ if ( opt . pkg . name === "hl7.fhir.r4.core" ) return true ;
44+ }
45+ }
46+ return false ;
47+ } ;
48+
49+ const fieldTypeResolutionHint = ( register : Register , pkg : PackageMeta , type : string ) : string => {
50+ if ( ! R5_ONLY_TYPES . has ( type ) ) return "" ;
51+ if ( ! dependsOnR4Core ( register , pkg ) ) return "" ;
52+ return (
53+ `\n hint: '${ type } ' is an R5+ type and is not available when generating against R4.` +
54+ `\n Either skip this canonical via skip-hack.ts, or upgrade the target to R5.`
55+ ) ;
56+ } ;
57+
2858function isRequired ( register : Register , fhirSchema : RichFHIRSchema , path : string [ ] ) : boolean {
2959 const fieldName = path [ path . length - 1 ] ;
3060 if ( ! fieldName ) throw new Error ( `Internal error: fieldName is missing for path ${ path . join ( "/" ) } ` ) ;
@@ -283,10 +313,18 @@ export function buildFieldType(
283313 } else if ( element . type ) {
284314 const url = register . ensureSpecializationCanonicalUrl ( element . type ) ;
285315 const fieldFs = register . resolveFs ( fhirSchema . package_meta , url ) ;
286- if ( ! fieldFs )
316+ if ( ! fieldFs ) {
317+ const pkgId = packageMetaToFhir ( fhirSchema . package_meta ) ;
318+ const fieldPath = path . join ( "." ) ;
319+ const hint = fieldTypeResolutionHint ( register , fhirSchema . package_meta , element . type ) ;
287320 throw new Error (
288- `Could not resolve field type: <${ fhirSchema . url } >.${ path . join ( "." ) } : <${ element . type } > (pkg: '${ packageMetaToFhir ( fhirSchema . package_meta ) } '))` ,
321+ `Could not resolve field type:
322+ package: ${ pkgId }
323+ schema: ${ fhirSchema . url }
324+ field: ${ fieldPath }
325+ type: ${ element . type } ${ hint } ` ,
289326 ) ;
327+ }
290328 return mkIdentifier ( fieldFs ) ;
291329 } else if ( element . choices ) {
292330 return undefined ;
0 commit comments