@@ -326,23 +326,26 @@ type structTemplateState struct {
326
326
PosLine string
327
327
Name string
328
328
Fields []string
329
+ Generics []string
329
330
Extends string
330
331
AboveLine string
331
332
}
332
333
333
334
const structTemplate = `{{ .PosLine -}}
334
335
{{ if .AboveLine }}{{ .AboveLine }}
335
- {{ end }}export interface {{ .Name }}{{ if .Extends }} extends {{ .Extends }}{{ end }}{
336
- {{- range .Fields }}
337
- {{ . -}}
338
- {{- end }}
336
+ {{ end }}export interface {{ .Name }}{{ if .Generics }}<{{ join .Generics ", " }}>{{ end }}{{ if .Extends }} extends {{ .Extends }}{{ end }} {
337
+ {{ join .Fields "\n"}}
339
338
}
340
339
`
341
340
342
341
// buildStruct just prints the typescript def for a type.
343
342
func (g * Generator ) buildStruct (obj types.Object , st * types.Struct ) (string , error ) {
344
343
state := structTemplateState {}
345
- tpl , err := template .New ("struct" ).Parse (structTemplate )
344
+ tpl := template .New ("struct" )
345
+ tpl .Funcs (template.FuncMap {
346
+ "join" : strings .Join ,
347
+ })
348
+ tpl , err := tpl .Parse (structTemplate )
346
349
if err != nil {
347
350
return "" , xerrors .Errorf ("parse struct template: %w" , err )
348
351
}
@@ -428,7 +431,12 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
428
431
if jsonOptional || tsType .Optional {
429
432
optional = "?"
430
433
}
431
- state .Fields = append (state .Fields , fmt .Sprintf ("%sreadonly %s%s: %s" , indent , jsonName , optional , tsType .ValueType ))
434
+ valueType := tsType .ValueType
435
+ if tsType .GenericMapping != "" {
436
+ valueType = tsType .GenericMapping
437
+ state .Generics = append (state .Generics , fmt .Sprintf ("%s extends %s" , tsType .GenericMapping , tsType .ValueType ))
438
+ }
439
+ state .Fields = append (state .Fields , fmt .Sprintf ("%sreadonly %s%s: %s" , indent , jsonName , optional , valueType ))
432
440
}
433
441
434
442
data := bytes .NewBuffer (make ([]byte , 0 ))
@@ -603,7 +611,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
603
611
604
612
generic := ty .Constraint ()
605
613
// This is kinda a hack, but we want just the end of the name.
606
- name := strings .TrimSuffix ( "github.com/coder/coder/codersdk." , generic . String () )
614
+ name := strings .TrimPrefix ( generic . String (), "github.com/coder/coder/codersdk." )
607
615
return TypescriptType {
608
616
GenericMapping : ty .Obj ().Name (),
609
617
ValueType : name ,
0 commit comments