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

Commit d443b17

Browse files
committed
Fix null types generation
1 parent 325fc6c commit d443b17

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

generate_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func TestGenerateWithImports(t *testing.T) {
4848
"imports/struct_shorthand": "Shorthand struct notation is currently unsupported, needs fixing",
4949
"imports/single_embed": "Single-item struct embeds should be treated as just another interface to compose, but get confused with references - #60",
5050
"imports/inline_comments": "Inline comments do not appear be retrievable from cue.Value.Doc()",
51-
"imports/nulltype": "null types are not handled correctly",
5251
},
5352
}
5453

generator.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,13 @@ func (g generator) tsprintField(v cue.Value, isType bool) (ts.Expr, error) {
12261226
return nil, valError(v, "bounds constraints are not supported as they lack a direct typescript equivalent")
12271227
}
12281228
fallthrough
1229-
case cue.FloatKind, cue.IntKind, cue.BoolKind, cue.NullKind, cue.StructKind:
1229+
case cue.NullKind:
1230+
// It evaluates single null value
1231+
if op == cue.NoOp && len(dvals) == 0 {
1232+
return tsprintType(cue.NullKind), nil
1233+
}
1234+
fallthrough
1235+
case cue.FloatKind, cue.IntKind, cue.BoolKind, cue.StructKind:
12301236
// Having eliminated the possibility of bounds/constraints, we're left
12311237
// with disjunctions and basic types.
12321238
switch op {
@@ -1292,6 +1298,8 @@ func tsprintType(k cue.Kind) ts.Expr {
12921298
return ts.Ident("number")
12931299
case cue.TopKind:
12941300
return ts.Ident("unknown")
1301+
case cue.NullKind:
1302+
return ts.Ident("null")
12951303
default:
12961304
return nil
12971305
}

testdata/imports/nulltype.txtar

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ obj: {
1515

1616
export type nullType = null;
1717

18-
export type nullInUnion = string | null;
18+
export type nullInUnion = (string | null);
1919

20-
export type nullDefault = "foo" | "bar" | null;
20+
export type nullDefault = ('foo' | 'bar' | null);
2121

2222
export const defaultnullDefault: nullDefault = null;
2323

2424
export interface obj {
25-
nullField: null
26-
}
25+
nullField: null;
26+
}

0 commit comments

Comments
 (0)