@@ -909,11 +909,10 @@ export function processCompleteProperty({ key, content }: { key?: string, conten
909909function processComplexTypeDeclaration ( declaration : string ) : string {
910910 // Handle union and intersection types
911911 if ( declaration . includes ( '|' ) || declaration . includes ( '&' ) ) {
912- const match = declaration . match ( REGEX . unionIntersection )
913- if ( match ) {
914- const types = declaration . split ( / \s * [ | & ] \s * / )
915- return types . join ( declaration . includes ( '|' ) ? ' | ' : ' & ' )
916- }
912+ const operator = declaration . includes ( '|' ) ? '|' : '&'
913+ const types = declaration . split ( new RegExp ( `\\s*\\${ operator } \\s*` ) ) . map ( type => type . trim ( ) )
914+ const combinedTypes = combineTypes ( types , operator as '|' | '&' )
915+ return combinedTypes
917916 }
918917
919918 // Handle mapped types
@@ -1018,6 +1017,23 @@ export function isFunction(value: string): boolean {
10181017 )
10191018}
10201019
1020+ /**
1021+ * Check if a given type string represents a function type
1022+ */
1023+ function isFunctionType ( type : string ) : boolean {
1024+ const functionTypeRegex = / ^ \s * \( .* \) \s * = > \s * (?: \S .* | [ \t \v \f \xA0 \u1680 \u2000 - \u200A \u202F \u205F \u3000 \uFEFF ] ) $ /
1025+ return functionTypeRegex . test ( type . trim ( ) )
1026+ }
1027+
1028+ /**
1029+ * Combine types into a union or intersection, wrapping function types in parentheses
1030+ */
1031+ function combineTypes ( types : string [ ] , operator : '|' | '&' = '|' ) : string {
1032+ const uniqueTypes = [ ...new Set ( types ) ]
1033+ const normalizedTypes = uniqueTypes . map ( type => isFunctionType ( type ) ? `(${ type } )` : type )
1034+ return normalizedTypes . join ( ` ${ operator } ` )
1035+ }
1036+
10211037/**
10221038 * Determines if a line is a comment
10231039 * @param line - Source code line to check
@@ -1079,14 +1095,8 @@ function inferArrayType(value: string): string {
10791095 }
10801096
10811097 const elementTypes = elements . map ( element => inferElementType ( element . trim ( ) ) )
1082- const uniqueTypes = [ ...new Set ( elementTypes ) ]
1083-
1084- // Handle nested arrays
1085- if ( uniqueTypes . every ( type => type . startsWith ( 'Array<' ) ) ) {
1086- return `Array<${ uniqueTypes . join ( ' | ' ) } >`
1087- }
1088-
1089- return `Array<${ uniqueTypes . join ( ' | ' ) } >`
1098+ const combinedTypes = combineTypes ( elementTypes )
1099+ return `Array<${ combinedTypes } >`
10901100}
10911101
10921102/**
@@ -1172,15 +1182,16 @@ export function processNestedArray(elements: string[]): string {
11721182 if ( nestedContent ) {
11731183 const nestedElements = splitArrayElements ( nestedContent )
11741184 const nestedTypes = nestedElements . map ( ne => inferElementType ( ne . trim ( ) ) )
1175- return `Array<${ nestedTypes . join ( ' | ' ) } >`
1185+ const combinedNestedTypes = combineTypes ( nestedTypes )
1186+ return `Array<${ combinedNestedTypes } >`
11761187 }
11771188 return 'never'
11781189 }
11791190
11801191 return inferElementType ( trimmed )
11811192 } ) . filter ( type => type !== 'never' )
11821193
1183- return processedTypes . join ( ' | ' )
1194+ return combineTypes ( processedTypes )
11841195}
11851196
11861197/**
0 commit comments