Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit 6d3f6f4

Browse files
committed
Map [string]: into their correct Result
1 parent c39ec1b commit 6d3f6f4

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

generator.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,11 +909,23 @@ func tsprintField(v cue.Value, isType bool) (ts.Expr, error) {
909909
case cue.StructKind:
910910
switch op {
911911
case cue.SelectorOp, cue.AndOp, cue.NoOp:
912+
val := v.LookupPath(cue.MakePath(cue.AnyString))
913+
if val.Exists() {
914+
kvs := []tsast.KeyValueExpr{
915+
{
916+
Key: ts.Ident("string"),
917+
Value: tsprintType(val.IncompleteKind()),
918+
CommentList: commentsFor(val.Value(), true),
919+
},
920+
}
921+
922+
return tsast.ObjectLit{Elems: kvs, IsType: isType, IsMap: true}, nil
923+
}
924+
912925
iter, err := v.Fields(cue.Optional(true))
913926
if err != nil {
914927
return nil, valError(v, "something went wrong when generate nested structs")
915928
}
916-
917929
size, _ := v.Len().Int64()
918930
kvs := make([]tsast.KeyValueExpr, 0, size)
919931
for iter.Next() {

testdata/map_to_type.txtar

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- cue --
2+
package cuetsy
3+
4+
Map: {
5+
boolTest: [string]: bool
6+
numberTest: [string]: int64
7+
stringTest: [string]: string
8+
} @cuetsy(kind="interface")
9+
10+
-- ts --
11+
12+
export interface Map {
13+
boolTest: Record<string, boolean>;
14+
numberTest: Record<string, number>;
15+
stringTest: Record<string, string>;
16+
}

ts/ast/lit.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type ObjectLit struct {
1010
Comment []Comment
1111
Elems []KeyValueExpr
1212
IsType bool
13+
IsMap bool
1314

1415
eol EOL
1516
lvl int
@@ -36,6 +37,12 @@ func (o ObjectLit) innerString(aeol EOL, lvl int) string {
3637
write(strings.Repeat(Indent, n))
3738
}
3839

40+
if o.IsMap {
41+
kv := o.Elems[0]
42+
write(fmt.Sprintf("Record<%s, %s>", kv.Key.String(), kv.Value.String()))
43+
return b.String()
44+
}
45+
3946
if len(o.Elems) == 0 {
4047
if o.IsType {
4148
write("Record<string, unknown>")

0 commit comments

Comments
 (0)