Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: Add generics to typescript generator #4658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ignore generics already declared
  • Loading branch information
Emyrk committed Oct 19, 2022
commit 247bb1eb70ba3e95d1c7c49c4d271ad83bdcd375
11 changes: 10 additions & 1 deletion scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
state.Extends = strings.Join(extends, ", ")
}

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