@@ -210,22 +210,36 @@ function plainToPropertyType(type: PlainPropertyType, union: UnionType | undefin
210210}
211211
212212export function mergePropertyTypes ( first : PlainPropertyType , second : PlainPropertyType ) : PlainPropertyType {
213- const flattenedFirst = flattenPlainType ( first ) ;
214- const flattenedSecond = flattenPlainType ( second ) ;
215- for ( const second of flattenedSecond ) {
216- if ( ! includesType ( flattenedFirst , second ) ) {
217- flattenedFirst . push ( second ) ;
218- }
213+ const { union : flattenedFirstUnion , array : flattenedFirstArray } = flattenPlainType ( first ) ;
214+ const { union : flattenedSecondUnion , array : flattenedSecondArray } = flattenPlainType ( second ) ;
215+ const flattenedUnion = mergeTypeUnion ( flattenedFirstUnion , flattenedSecondUnion ) ;
216+ const flattenedArray = mergeTypeUnion ( flattenedFirstArray , flattenedSecondArray ) ;
217+ if ( flattenedArray . length > 0 ) {
218+ flattenedUnion . push ( {
219+ elementType : flattenedArray . length === 1 ? flattenedArray [ 0 ] : {
220+ types : flattenedArray
221+ }
222+ } ) ;
219223 }
220- if ( flattenedFirst . length === 1 ) {
221- return flattenedFirst [ 0 ] ;
224+ if ( flattenedUnion . length === 1 ) {
225+ return flattenedUnion [ 0 ] ;
222226 } else {
223227 return {
224- types : flattenedFirst
228+ types : flattenedUnion
225229 } ;
226230 }
227231}
228232
233+ function mergeTypeUnion ( first : PlainPropertyType [ ] , second : PlainPropertyType [ ] ) : PlainPropertyType [ ] {
234+ const result = [ ...first ] ;
235+ for ( const type of second ) {
236+ if ( ! includesType ( result , type ) ) {
237+ result . push ( type ) ;
238+ }
239+ }
240+ return result ;
241+ }
242+
229243function includesType ( list : PlainPropertyType [ ] , value : PlainPropertyType ) : boolean {
230244 return list . some ( e => typeEquals ( e , value ) ) ;
231245}
@@ -244,10 +258,22 @@ function typeEquals(first: PlainPropertyType, second: PlainPropertyType): boolea
244258 }
245259}
246260
247- export function flattenPlainType ( type : PlainPropertyType ) : PlainPropertyType [ ] {
261+ export function flattenPlainType ( type : PlainPropertyType ) : { union : PlainPropertyType [ ] , array : PlainPropertyType [ ] } {
248262 if ( isPlainPropertyUnion ( type ) ) {
249- return type . types . flatMap ( e => flattenPlainType ( e ) ) ;
263+ const flattened = type . types . flatMap ( e => flattenPlainType ( e ) ) ;
264+ return {
265+ union : flattened . map ( e => e . union ) . flat ( ) ,
266+ array : flattened . map ( e => e . array ) . flat ( )
267+ } ;
268+ } else if ( isPlainArrayType ( type ) ) {
269+ return {
270+ array : flattenPlainType ( type . elementType ) . union ,
271+ union : [ ]
272+ } ;
250273 } else {
251- return [ type ] ;
274+ return {
275+ array : [ ] ,
276+ union : [ type ]
277+ } ;
252278 }
253279}
0 commit comments