@@ -18,6 +18,7 @@ export type TSGenOptions = {
1818 } ;
1919 systemFields ?: boolean ;
2020 isEditableTags ?: boolean ;
21+ includeReferencedEntry ?: boolean ;
2122} ;
2223
2324export type TSGenResult = {
@@ -74,6 +75,7 @@ const defaultOptions: TSGenOptions = {
7475 } ,
7576 systemFields : false ,
7677 isEditableTags : false ,
78+ includeReferencedEntry : false ,
7779} ;
7880
7981export default function ( userOptions : TSGenOptions ) {
@@ -534,6 +536,33 @@ export default function (userOptions: TSGenOptions) {
534536 return name_type ( field . reference_to ) ;
535537 }
536538
539+ function buildReferenceArrayType ( references : string [ ] , options : any ) : string {
540+ // If no valid references remain, return a more specific fallback type
541+ if ( references . length === 0 ) {
542+ return "Record<string, unknown>[]" ;
543+ }
544+
545+ // Handle reference types with or without ReferencedEntry interface
546+ if ( options . includeReferencedEntry ) {
547+ const referencedEntryType = `${ options . naming ?. prefix || "" } ReferencedEntry` ;
548+
549+ const wrapWithReferencedEntry = ( refType : string ) =>
550+ `(${ refType } | ${ referencedEntryType } )` ;
551+
552+ const types =
553+ references . length === 1
554+ ? wrapWithReferencedEntry ( references [ 0 ] )
555+ : references . map ( wrapWithReferencedEntry ) . join ( " | " ) ;
556+
557+ return `${ types } []` ;
558+ }
559+
560+ const baseType =
561+ references . length === 1 ? references [ 0 ] : references . join ( " | " ) ;
562+
563+ return `${ baseType } []` ;
564+ }
565+
537566 function type_reference ( field : ContentstackTypes . Field ) {
538567 const references : string [ ] = [ ] ;
539568
@@ -561,25 +590,7 @@ export default function (userOptions: TSGenOptions) {
561590 }
562591 }
563592
564- // If no valid references remain, return a more specific fallback type
565- if ( references . length === 0 ) {
566- return "Record<string, unknown>[]" ;
567- }
568-
569- // Use the ReferencedEntry interface from builtins
570- const referencedEntryType = `${ options . naming ?. prefix || "" } ReferencedEntry` ;
571-
572- // If there's only one reference type, create a simple union
573- if ( references . length === 1 ) {
574- return `(${ references [ 0 ] } | ${ referencedEntryType } )[]` ;
575- }
576-
577- // If there are multiple reference types, create separate unions for each
578- const unionTypes = references . map ( ( refType ) => {
579- return `(${ refType } | ${ referencedEntryType } )` ;
580- } ) ;
581-
582- return `${ unionTypes . join ( " | " ) } []` ;
593+ return buildReferenceArrayType ( references , options ) ;
583594 }
584595
585596 return function (
0 commit comments