@@ -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
}
@@ -432,7 +435,12 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
432
435
if jsonOptional || tsType .Optional {
433
436
optional = "?"
434
437
}
435
- state .Fields = append (state .Fields , fmt .Sprintf ("%sreadonly %s%s: %s" , indent , jsonName , optional , tsType .ValueType ))
438
+ valueType := tsType .ValueType
439
+ if tsType .GenericMapping != "" {
440
+ valueType = tsType .GenericMapping
441
+ state .Generics = append (state .Generics , fmt .Sprintf ("%s extends %s" , tsType .GenericMapping , tsType .ValueType ))
442
+ }
443
+ state .Fields = append (state .Fields , fmt .Sprintf ("%sreadonly %s%s: %s" , indent , jsonName , optional , valueType ))
436
444
}
437
445
438
446
data := bytes .NewBuffer (make ([]byte , 0 ))
@@ -607,7 +615,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
607
615
608
616
generic := ty .Constraint ()
609
617
// This is kinda a hack, but we want just the end of the name.
610
- name := strings .TrimSuffix ( "github.com/coder/coder/codersdk." , generic . String () )
618
+ name := strings .TrimPrefix ( generic . String (), "github.com/coder/coder/codersdk." )
611
619
return TypescriptType {
612
620
GenericMapping : ty .Obj ().Name (),
613
621
ValueType : name ,
0 commit comments