@@ -481,7 +481,9 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
481
481
valueType := tsType .ValueType
482
482
if tsType .GenericValue != "" {
483
483
valueType = tsType .GenericValue
484
- for name , constraint := range tsType .GenericTypes {
484
+
485
+ for _ , value := range tsType .GenericTypes {
486
+ name , constraint := value .Name , value .Constraint
485
487
if _ , ok := genericsUsed [name ]; ok {
486
488
// Don't add a generic twice
487
489
// TODO: We should probably check that the generic mapping is
@@ -510,12 +512,19 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
510
512
return data .String (), nil
511
513
}
512
514
515
+ type GenericType struct {
516
+ // Name is the generic name, eg "C"
517
+ Name string
518
+ // Constraint is the interface constraint, eg "comparable"
519
+ Constraint string
520
+ }
521
+
513
522
type TypescriptType struct {
514
- // GenericTypes is a map of generic name to actual constraint.
523
+ // GenericTypes are a list of the generic names to actual constraint.
515
524
// We return these, so we can bubble them up if we are recursively traversing
516
525
// a nested structure. We duplicate these at the top level.
517
- // Example: 'C = comparable'.
518
- GenericTypes map [ string ] string
526
+ // Example: '{Name: "C", Constraint: " comparable"} '.
527
+ GenericTypes [] GenericType
519
528
// GenericValue is the value using the Generic name, rather than the constraint.
520
529
// This is only usedful if you can use the generic syntax. Things like maps
521
530
// don't currently support this, and will use the ValueType instead.
@@ -647,7 +656,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
647
656
// we generate.
648
657
name := n .Obj ().Name ()
649
658
genericName := ""
650
- genericTypes := make ( map [ string ] string )
659
+ var genericTypes [] GenericType
651
660
if obj := g .pkg .Types .Scope ().Lookup (name ); obj != nil {
652
661
// Sweet! Using other typescript types as fields. This could be an
653
662
// enum or another struct
@@ -664,7 +673,10 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
664
673
// Using a generic defined by the parent.
665
674
gname := param .Obj ().Name ()
666
675
genericNames = append (genericNames , gname )
667
- genericTypes [gname ] = genType .ValueType
676
+ genericTypes = append (genericTypes , GenericType {
677
+ Name : gname ,
678
+ Constraint : genType .ValueType ,
679
+ })
668
680
} else {
669
681
// Defining a generic
670
682
genericNames = append (genericNames , genType .ValueType )
@@ -736,9 +748,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
736
748
// If we don't have the type constraint defined somewhere in the package,
737
749
// then we have to resort to using any.
738
750
return TypescriptType {
739
- GenericTypes : map [string ]string {
740
- ty .Obj ().Name (): "any" ,
741
- },
751
+ GenericTypes : []GenericType {{Name : ty .Obj ().Name (), Constraint : "any" }},
742
752
GenericValue : ty .Obj ().Name (),
743
753
ValueType : "any" ,
744
754
AboveTypeLine : fmt .Sprintf ("// %q is an external type, so we use any" , name ),
@@ -750,9 +760,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
750
760
}
751
761
752
762
return TypescriptType {
753
- GenericTypes : map [string ]string {
754
- ty .Obj ().Name (): name ,
755
- },
763
+ GenericTypes : []GenericType {{Name : ty .Obj ().Name (), Constraint : name }},
756
764
GenericValue : ty .Obj ().Name (),
757
765
ValueType : name ,
758
766
AboveTypeLine : "" ,
0 commit comments