@@ -17,6 +17,7 @@ export type TSGenOptions = {
1717 } ;
1818 systemFields ?: boolean ;
1919 isEditableTags ?: boolean ;
20+ includeReferencedEntry ?: boolean ;
2021} ;
2122
2223export type TSGenResult = {
@@ -73,6 +74,7 @@ const defaultOptions: TSGenOptions = {
7374 } ,
7475 systemFields : false ,
7576 isEditableTags : false ,
77+ includeReferencedEntry : false ,
7678} ;
7779
7880export default function ( userOptions : TSGenOptions ) {
@@ -493,6 +495,33 @@ export default function (userOptions: TSGenOptions) {
493495 return name_type ( field . reference_to ) ;
494496 }
495497
498+ function buildReferenceArrayType ( references : string [ ] , options : any ) : string {
499+ // If no valid references remain, return a more specific fallback type
500+ if ( references . length === 0 ) {
501+ return "Record<string, unknown>[]" ;
502+ }
503+
504+ // Handle reference types with or without ReferencedEntry interface
505+ if ( options . includeReferencedEntry ) {
506+ const referencedEntryType = `${ options . naming ?. prefix || "" } ReferencedEntry` ;
507+
508+ const wrapWithReferencedEntry = ( refType : string ) =>
509+ `(${ refType } | ${ referencedEntryType } )` ;
510+
511+ const types =
512+ references . length === 1
513+ ? wrapWithReferencedEntry ( references [ 0 ] )
514+ : references . map ( wrapWithReferencedEntry ) . join ( " | " ) ;
515+
516+ return `${ types } []` ;
517+ }
518+
519+ const baseType =
520+ references . length === 1 ? references [ 0 ] : references . join ( " | " ) ;
521+
522+ return `${ baseType } []` ;
523+ }
524+
496525 function type_reference ( field : ContentstackTypes . Field ) {
497526 const references : string [ ] = [ ] ;
498527
@@ -520,25 +549,7 @@ export default function (userOptions: TSGenOptions) {
520549 }
521550 }
522551
523- // If no valid references remain, return a more specific fallback type
524- if ( references . length === 0 ) {
525- return "Record<string, unknown>[]" ;
526- }
527-
528- // Use the ReferencedEntry interface from builtins
529- const referencedEntryType = `${ options . naming ?. prefix || "" } ReferencedEntry` ;
530-
531- // If there's only one reference type, create a simple union
532- if ( references . length === 1 ) {
533- return `(${ references [ 0 ] } | ${ referencedEntryType } )[]` ;
534- }
535-
536- // If there are multiple reference types, create separate unions for each
537- const unionTypes = references . map ( ( refType ) => {
538- return `(${ refType } | ${ referencedEntryType } )` ;
539- } ) ;
540-
541- return `${ unionTypes . join ( " | " ) } []` ;
552+ return buildReferenceArrayType ( references , options ) ;
542553 }
543554
544555 return function (
0 commit comments