Skip to content

chore: Add generics to typescript generator #4664

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

Merged
merged 23 commits into from
Oct 20, 2022
Merged
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
Ensure constant ordering
  • Loading branch information
Emyrk committed Oct 20, 2022
commit a18fe72911edc00499e671c58dace3f8fe40d36c
32 changes: 20 additions & 12 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
valueType := tsType.ValueType
if tsType.GenericValue != "" {
valueType = tsType.GenericValue
for name, constraint := range tsType.GenericTypes {

for _, value := range tsType.GenericTypes {
name, constraint := value.Name, value.Constraint
if _, ok := genericsUsed[name]; ok {
// Don't add a generic twice
// TODO: We should probably check that the generic mapping is
Expand Down Expand Up @@ -510,12 +512,19 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
return data.String(), nil
}

type GenericType struct {
// Name is the generic name, eg "C"
Name string
// Constraint is the interface constraint, eg "comparable"
Constraint string
}

type TypescriptType struct {
// GenericTypes is a map of generic name to actual constraint.
// GenericTypes are a list of the generic names to actual constraint.
// We return these, so we can bubble them up if we are recursively traversing
// a nested structure. We duplicate these at the top level.
// Example: 'C = comparable'.
GenericTypes map[string]string
// Example: '{Name: "C", Constraint: "comparable"}'.
GenericTypes []GenericType
// GenericValue is the value using the Generic name, rather than the constraint.
// This is only usedful if you can use the generic syntax. Things like maps
// don't currently support this, and will use the ValueType instead.
Expand Down Expand Up @@ -647,7 +656,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
// we generate.
name := n.Obj().Name()
genericName := ""
genericTypes := make(map[string]string)
var genericTypes []GenericType
if obj := g.pkg.Types.Scope().Lookup(name); obj != nil {
// Sweet! Using other typescript types as fields. This could be an
// enum or another struct
Expand All @@ -664,7 +673,10 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
// Using a generic defined by the parent.
gname := param.Obj().Name()
genericNames = append(genericNames, gname)
genericTypes[gname] = genType.ValueType
genericTypes = append(genericTypes, GenericType{
Name: gname,
Constraint: genType.ValueType,
})
} else {
// Defining a generic
genericNames = append(genericNames, genType.ValueType)
Expand Down Expand Up @@ -736,9 +748,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
// If we don't have the type constraint defined somewhere in the package,
// then we have to resort to using any.
return TypescriptType{
GenericTypes: map[string]string{
ty.Obj().Name(): "any",
},
GenericTypes: []GenericType{{Name: ty.Obj().Name(), Constraint: "any"}},
GenericValue: ty.Obj().Name(),
ValueType: "any",
AboveTypeLine: fmt.Sprintf("// %q is an external type, so we use any", name),
Expand All @@ -750,9 +760,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
}

return TypescriptType{
GenericTypes: map[string]string{
ty.Obj().Name(): name,
},
GenericTypes: []GenericType{{Name: ty.Obj().Name(), Constraint: name}},
GenericValue: ty.Obj().Name(),
ValueType: name,
AboveTypeLine: "",
Expand Down