From d443b17a8dd159f313e93f13be2e0db86a89fd13 Mon Sep 17 00:00:00 2001 From: spinillos Date: Fri, 31 Mar 2023 15:43:28 +0200 Subject: [PATCH] Fix null types generation --- generate_test.go | 1 - generator.go | 10 +++++++++- testdata/imports/nulltype.txtar | 8 ++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/generate_test.go b/generate_test.go index 16a3737..a9d2e4d 100644 --- a/generate_test.go +++ b/generate_test.go @@ -48,7 +48,6 @@ func TestGenerateWithImports(t *testing.T) { "imports/struct_shorthand": "Shorthand struct notation is currently unsupported, needs fixing", "imports/single_embed": "Single-item struct embeds should be treated as just another interface to compose, but get confused with references - #60", "imports/inline_comments": "Inline comments do not appear be retrievable from cue.Value.Doc()", - "imports/nulltype": "null types are not handled correctly", }, } diff --git a/generator.go b/generator.go index 504bb6e..818cd32 100644 --- a/generator.go +++ b/generator.go @@ -1226,7 +1226,13 @@ func (g generator) tsprintField(v cue.Value, isType bool) (ts.Expr, error) { return nil, valError(v, "bounds constraints are not supported as they lack a direct typescript equivalent") } fallthrough - case cue.FloatKind, cue.IntKind, cue.BoolKind, cue.NullKind, cue.StructKind: + case cue.NullKind: + // It evaluates single null value + if op == cue.NoOp && len(dvals) == 0 { + return tsprintType(cue.NullKind), nil + } + fallthrough + case cue.FloatKind, cue.IntKind, cue.BoolKind, cue.StructKind: // Having eliminated the possibility of bounds/constraints, we're left // with disjunctions and basic types. switch op { @@ -1292,6 +1298,8 @@ func tsprintType(k cue.Kind) ts.Expr { return ts.Ident("number") case cue.TopKind: return ts.Ident("unknown") + case cue.NullKind: + return ts.Ident("null") default: return nil } diff --git a/testdata/imports/nulltype.txtar b/testdata/imports/nulltype.txtar index 656bb7a..14633cf 100644 --- a/testdata/imports/nulltype.txtar +++ b/testdata/imports/nulltype.txtar @@ -15,12 +15,12 @@ obj: { export type nullType = null; -export type nullInUnion = string | null; +export type nullInUnion = (string | null); -export type nullDefault = "foo" | "bar" | null; +export type nullDefault = ('foo' | 'bar' | null); export const defaultnullDefault: nullDefault = null; export interface obj { - nullField: null -} \ No newline at end of file + nullField: null; +}