@@ -258,7 +258,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
258
258
tsType .ValueType = typescriptTag
259
259
} else {
260
260
var err error
261
- tsType , err = g .typescriptType (obj , field .Type ())
261
+ tsType , err = g .typescriptType (field .Type ())
262
262
if err != nil {
263
263
return "" , xerrors .Errorf ("typescript type: %w" , err )
264
264
}
@@ -288,7 +288,7 @@ type TypescriptType struct {
288
288
// golang type.
289
289
// Eg:
290
290
// []byte returns "string"
291
- func (g * Generator ) typescriptType (obj types. Object , ty types.Type ) (TypescriptType , error ) {
291
+ func (g * Generator ) typescriptType (ty types.Type ) (TypescriptType , error ) {
292
292
switch ty .(type ) {
293
293
case * types.Basic :
294
294
bs := ty .(* types.Basic )
@@ -312,7 +312,20 @@ func (g *Generator) typescriptType(obj types.Object, ty types.Type) (TypescriptT
312
312
return TypescriptType {ValueType : ty .String (), Comment : "Unknown struct, this might not work" }, nil
313
313
case * types.Map :
314
314
// TODO: Typescript dictionary??? Object?
315
- return TypescriptType {ValueType : "map_not_implemented" }, nil
315
+ // map[string][string] -> Record<string, string>
316
+ m := ty .(* types.Map )
317
+ keyType , err := g .typescriptType (m .Key ())
318
+ if err != nil {
319
+ return TypescriptType {}, xerrors .Errorf ("map key: %w" , err )
320
+ }
321
+ valueType , err := g .typescriptType (m .Elem ())
322
+ if err != nil {
323
+ return TypescriptType {}, xerrors .Errorf ("map key: %w" , err )
324
+ }
325
+
326
+ return TypescriptType {
327
+ ValueType : fmt .Sprintf ("Record<%s, %s>" , keyType .ValueType , valueType .ValueType ),
328
+ }, nil
316
329
case * types.Slice , * types.Array :
317
330
// Slice/Arrays are pretty much the same.
318
331
type hasElem interface {
@@ -329,7 +342,7 @@ func (g *Generator) typescriptType(obj types.Object, ty types.Type) (TypescriptT
329
342
return TypescriptType {ValueType : "string" }, nil
330
343
default :
331
344
// By default, just do an array of the underlying type.
332
- underlying , err := g .typescriptType (obj , arr .Elem ())
345
+ underlying , err := g .typescriptType (arr .Elem ())
333
346
if err != nil {
334
347
return TypescriptType {}, xerrors .Errorf ("array: %w" , err )
335
348
}
@@ -362,13 +375,13 @@ func (g *Generator) typescriptType(obj types.Object, ty types.Type) (TypescriptT
362
375
}
363
376
364
377
// Defer to the underlying type.
365
- return g .typescriptType (obj , ty .Underlying ())
378
+ return g .typescriptType (ty .Underlying ())
366
379
case * types.Pointer :
367
380
// Dereference pointers.
368
381
// TODO: Nullable fields? We could say these fields can be null in the
369
382
// typescript.
370
383
pt := ty .(* types.Pointer )
371
- resp , err := g .typescriptType (obj , pt .Elem ())
384
+ resp , err := g .typescriptType (pt .Elem ())
372
385
if err != nil {
373
386
return TypescriptType {}, xerrors .Errorf ("pointer: %w" , err )
374
387
}
0 commit comments