@@ -481,7 +481,9 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
481481 valueType := tsType .ValueType
482482 if tsType .GenericValue != "" {
483483 valueType = tsType .GenericValue
484- for name , constraint := range tsType .GenericTypes {
484+
485+ for _ , value := range tsType .GenericTypes {
486+ name , constraint := value .Name , value .Constraint
485487 if _ , ok := genericsUsed [name ]; ok {
486488 // Don't add a generic twice
487489 // 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
510512 return data .String (), nil
511513}
512514
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+
513522type 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.
515524 // We return these, so we can bubble them up if we are recursively traversing
516525 // 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
519528 // GenericValue is the value using the Generic name, rather than the constraint.
520529 // This is only usedful if you can use the generic syntax. Things like maps
521530 // don't currently support this, and will use the ValueType instead.
@@ -647,7 +656,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
647656 // we generate.
648657 name := n .Obj ().Name ()
649658 genericName := ""
650- genericTypes := make ( map [ string ] string )
659+ var genericTypes [] GenericType
651660 if obj := g .pkg .Types .Scope ().Lookup (name ); obj != nil {
652661 // Sweet! Using other typescript types as fields. This could be an
653662 // enum or another struct
@@ -664,7 +673,10 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
664673 // Using a generic defined by the parent.
665674 gname := param .Obj ().Name ()
666675 genericNames = append (genericNames , gname )
667- genericTypes [gname ] = genType .ValueType
676+ genericTypes = append (genericTypes , GenericType {
677+ Name : gname ,
678+ Constraint : genType .ValueType ,
679+ })
668680 } else {
669681 // Defining a generic
670682 genericNames = append (genericNames , genType .ValueType )
@@ -736,9 +748,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
736748 // If we don't have the type constraint defined somewhere in the package,
737749 // then we have to resort to using any.
738750 return TypescriptType {
739- GenericTypes : map [string ]string {
740- ty .Obj ().Name (): "any" ,
741- },
751+ GenericTypes : []GenericType {{Name : ty .Obj ().Name (), Constraint : "any" }},
742752 GenericValue : ty .Obj ().Name (),
743753 ValueType : "any" ,
744754 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) {
750760 }
751761
752762 return TypescriptType {
753- GenericTypes : map [string ]string {
754- ty .Obj ().Name (): name ,
755- },
763+ GenericTypes : []GenericType {{Name : ty .Obj ().Name (), Constraint : name }},
756764 GenericValue : ty .Obj ().Name (),
757765 ValueType : name ,
758766 AboveTypeLine : "" ,
0 commit comments