diff --git a/generator.go b/generator.go index a20b7ec..637500f 100644 --- a/generator.go +++ b/generator.go @@ -911,9 +911,10 @@ func tsprintField(v cue.Value, isType bool) (ts.Expr, error) { case cue.StructKind: switch op { case cue.SelectorOp, cue.AndOp, cue.NoOp: - // Checks [string]something and {...} + // Checks [string]something only. + // It skips structs like {...} (cue.TopKind) to avoid undesired results. val := v.LookupPath(cue.MakePath(cue.AnyString)) - if val.Exists() { + if val.Exists() && val.IncompleteKind() != cue.TopKind { expr, err := tsprintField(val, isType) if err != nil { return nil, valError(v, err.Error()) @@ -1122,7 +1123,7 @@ func tsprintType(k cue.Kind) ts.Expr { case cue.NumberKind, cue.FloatKind, cue.IntKind: return ts.Ident("number") case cue.TopKind: - return ts.Ident("unknown") + return ts.Ident("any") default: return nil } diff --git a/testdata/array_with_structs.txtar b/testdata/array_with_structs.txtar new file mode 100644 index 0000000..d1d44d2 --- /dev/null +++ b/testdata/array_with_structs.txtar @@ -0,0 +1,29 @@ +-- cue -- +package cuetsy + +List: { + test?: [...(#StructTest | #DefinedStructTest)] + + #StructTest: { + a: string + } + + #DefinedStructTest: { + type: "hola" + ... + } +} @cuetsy(kind="interface") + +-- ts -- + +export interface List { + test?: Array<({ + a: string; + } | { + type: 'hola'; + })>; +} + +export const defaultList: Partial = { + test: [], +}; diff --git a/testdata/interfaces.txtar b/testdata/interfaces.txtar index f47012c..7d55d9b 100644 --- a/testdata/interfaces.txtar +++ b/testdata/interfaces.txtar @@ -55,7 +55,7 @@ export enum E2 { export interface I1 { I1_FloatLiteral: 4.4; I1_OptionalDisjunctionLiteral?: ('other' | 'values' | 2); - I1_Top: unknown; + I1_Top: any; } export interface I2 { diff --git a/testdata/map_to_type.txtar b/testdata/map_to_type.txtar index 6194d6c..54673b5 100644 --- a/testdata/map_to_type.txtar +++ b/testdata/map_to_type.txtar @@ -5,6 +5,11 @@ package cuetsy a: string } +#StructWithDots: { + type: "hola" + ... +} + Map: { boolTest: [string]: bool numberTest: [string]: int64 @@ -15,6 +20,8 @@ Map: { mapTest: [string]: [string]: string structTest: [string]: #StructTest optionalTest?: [string]: string + emptyStructMapTest: [string]: {...} + structWithDotsTest: [string]: #StructWithDots } @cuetsy(kind="interface") @@ -23,6 +30,7 @@ Map: { export interface Map { boolTest: Record; + emptyStructMapTest: Record>; emptyStructTest: Record; listTest: Record>; listWithStructTest: Record; structTest: Record; + structWithDotsTest: Record; }