Skip to content

Commit 247bb1e

Browse files
committed
Ignore generics already declared
1 parent f955150 commit 247bb1e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

scripts/apitypings/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
370370
state.Extends = strings.Join(extends, ", ")
371371
}
372372

373+
genericsUsed := make(map[string]string)
373374
// For each field in the struct, we print 1 line of the typescript interface
374375
for i := 0; i < st.NumFields(); i++ {
375376
if extendedFields[i] {
@@ -438,7 +439,15 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
438439
valueType := tsType.ValueType
439440
if tsType.GenericMapping != "" {
440441
valueType = tsType.GenericMapping
441-
state.Generics = append(state.Generics, fmt.Sprintf("%s extends %s", tsType.GenericMapping, tsType.ValueType))
442+
// Don't add a generic twice
443+
if _, ok := genericsUsed[tsType.GenericMapping]; !ok {
444+
// TODO: We should probably check that the generic mapping is
445+
// not a different type. Like 'T' being referenced to 2 different
446+
// constraints. I don't think this is possible though in valid
447+
// go, so I'm going to ignore this for now.
448+
state.Generics = append(state.Generics, fmt.Sprintf("%s extends %s", tsType.GenericMapping, tsType.ValueType))
449+
}
450+
genericsUsed[tsType.GenericMapping] = tsType.ValueType
442451
}
443452
state.Fields = append(state.Fields, fmt.Sprintf("%sreadonly %s%s: %s", indent, jsonName, optional, valueType))
444453
}

0 commit comments

Comments
 (0)