@@ -509,12 +509,19 @@ <h3>粘贴 JSON 文本</h3>
509509 if ( types . length === 0 ) return scalarType ( 'any' ) ;
510510 if ( types . length === 1 ) return types [ 0 ] ;
511511
512- // 分离标量、数组、对象、联合
512+ // 先展开嵌套的联合类型
513+ const flat = [ ] ;
514+ function pushType ( t ) {
515+ if ( t . kind === 'union' ) t . types . forEach ( pushType ) ;
516+ else flat . push ( t ) ;
517+ }
518+ types . forEach ( pushType ) ;
519+
520+ // 分离标量、数组、对象
513521 const scalars = [ ] ;
514522 const arrays = [ ] ;
515523 const objects = [ ] ;
516- for ( const t of types ) {
517- if ( t . kind === 'union' ) { mergeTypes ( t . types ) ; continue ; }
524+ for ( const t of flat ) {
518525 if ( t . kind === 'scalar' ) scalars . push ( t ) ;
519526 else if ( t . kind === 'array' ) arrays . push ( t ) ;
520527 else if ( t . kind === 'object' ) objects . push ( t ) ;
@@ -609,7 +616,7 @@ <h3>粘贴 JSON 文本</h3>
609616 }
610617 if ( t . kind === 'array' ) {
611618 const item = tsTypeName ( t . item , fallbackName + 'Item' ) ;
612- return item + '[]' ;
619+ return ( item . includes ( '|' ) ? '(' + item + ')' : item ) + '[]' ;
613620 }
614621 if ( t . kind === 'object' ) {
615622 const name = fallbackName ;
@@ -645,7 +652,12 @@ <h3>粘贴 JSON 文本</h3>
645652 return tsTypeName ( t , fallbackName ) ;
646653 }
647654
648- tsTypeName ( type , rootName ) ;
655+ if ( type . kind === 'object' ) {
656+ tsTypeName ( type , rootName ) ;
657+ } else {
658+ const rootTypeStr = tsTypeName ( type , rootName ) ;
659+ defs . push ( `type ${ rootName } = ${ rootTypeStr } ;` ) ;
660+ }
649661 return defs ;
650662}
651663
@@ -705,7 +717,12 @@ <h3>粘贴 JSON 文本</h3>
705717
706718 defs . push ( 'package main' ) ;
707719 defs . push ( '' ) ;
708- goTypeName ( type , rootName , '' ) ;
720+ if ( type . kind === 'object' ) {
721+ goTypeName ( type , rootName , '' ) ;
722+ } else {
723+ const rootTypeStr = goTypeName ( type , rootName , '' ) ;
724+ defs . push ( `type ${ rootName } ${ rootTypeStr } ` ) ;
725+ }
709726 return defs ;
710727}
711728
@@ -770,15 +787,21 @@ <h3>粘贴 JSON 文本</h3>
770787 return pyTypeName ( t , fallbackName ) ;
771788 }
772789
773- if ( needsOptional || needsAny ) {
774- const imports = [ ] ;
775- if ( needsAny ) imports . push ( 'Any' ) ;
776- if ( needsOptional ) imports . push ( 'Optional' ) ;
777- defs . push ( `from typing import ${ imports . join ( ', ' ) } ` ) ;
790+ if ( type . kind === 'object' ) {
791+ pyTypeName ( type , rootName ) ;
792+ } else {
793+ const rootTypeStr = pyTypeName ( type , rootName ) ;
794+ defs . push ( `${ rootName } = ${ rootTypeStr } ` ) ;
778795 }
779- defs . push ( 'from dataclasses import dataclass' ) ;
780- defs . push ( '' ) ;
781- pyTypeName ( type , rootName ) ;
796+
797+ const imports = [ ] ;
798+ if ( needsAny ) imports . push ( 'Any' ) ;
799+ if ( needsOptional ) imports . push ( 'Optional' ) ;
800+ const head = [ 'from dataclasses import dataclass' ] ;
801+ if ( imports . length ) head . unshift ( `from typing import ${ imports . join ( ', ' ) } ` ) ;
802+ head . push ( '' ) ;
803+ defs . unshift ( ...head ) ;
804+
782805 return defs ;
783806}
784807
0 commit comments