Skip to content

Commit 7c94c8b

Browse files
committed
Ignore generics already declared
1 parent 6163ace commit 7c94c8b

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] {
@@ -434,7 +435,15 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
434435
valueType := tsType.ValueType
435436
if tsType.GenericMapping != "" {
436437
valueType = tsType.GenericMapping
437-
state.Generics = append(state.Generics, fmt.Sprintf("%s extends %s", tsType.GenericMapping, tsType.ValueType))
438+
// Don't add a generic twice
439+
if _, ok := genericsUsed[tsType.GenericMapping]; !ok {
440+
// TODO: We should probably check that the generic mapping is
441+
// not a different type. Like 'T' being referenced to 2 different
442+
// constraints. I don't think this is possible though in valid
443+
// go, so I'm going to ignore this for now.
444+
state.Generics = append(state.Generics, fmt.Sprintf("%s extends %s", tsType.GenericMapping, tsType.ValueType))
445+
}
446+
genericsUsed[tsType.GenericMapping] = tsType.ValueType
438447
}
439448
state.Fields = append(state.Fields, fmt.Sprintf("%sreadonly %s%s: %s", indent, jsonName, optional, valueType))
440449
}

0 commit comments

Comments
 (0)