@@ -909,11 +909,10 @@ export function processCompleteProperty({ key, content }: { key?: string, conten
909
909
function processComplexTypeDeclaration ( declaration : string ) : string {
910
910
// Handle union and intersection types
911
911
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
917
916
}
918
917
919
918
// Handle mapped types
@@ -1018,6 +1017,23 @@ export function isFunction(value: string): boolean {
1018
1017
)
1019
1018
}
1020
1019
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
+
1021
1037
/**
1022
1038
* Determines if a line is a comment
1023
1039
* @param line - Source code line to check
@@ -1079,14 +1095,8 @@ function inferArrayType(value: string): string {
1079
1095
}
1080
1096
1081
1097
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 } >`
1090
1100
}
1091
1101
1092
1102
/**
@@ -1172,15 +1182,16 @@ export function processNestedArray(elements: string[]): string {
1172
1182
if ( nestedContent ) {
1173
1183
const nestedElements = splitArrayElements ( nestedContent )
1174
1184
const nestedTypes = nestedElements . map ( ne => inferElementType ( ne . trim ( ) ) )
1175
- return `Array<${ nestedTypes . join ( ' | ' ) } >`
1185
+ const combinedNestedTypes = combineTypes ( nestedTypes )
1186
+ return `Array<${ combinedNestedTypes } >`
1176
1187
}
1177
1188
return 'never'
1178
1189
}
1179
1190
1180
1191
return inferElementType ( trimmed )
1181
1192
} ) . filter ( type => type !== 'never' )
1182
1193
1183
- return processedTypes . join ( ' | ' )
1194
+ return combineTypes ( processedTypes )
1184
1195
}
1185
1196
1186
1197
/**
0 commit comments