diff --git a/src/TypeFormatter/LiteralUnionTypeFormatter.ts b/src/TypeFormatter/LiteralUnionTypeFormatter.ts index e7cf4d844..51935c98a 100644 --- a/src/TypeFormatter/LiteralUnionTypeFormatter.ts +++ b/src/TypeFormatter/LiteralUnionTypeFormatter.ts @@ -18,6 +18,7 @@ export class LiteralUnionTypeFormatter implements SubTypeFormatter { let hasString = false; let preserveLiterals = false; let allStrings = true; + let hasNull = false; const flattenedTypes = flattenTypes(type); @@ -27,9 +28,10 @@ export class LiteralUnionTypeFormatter implements SubTypeFormatter { hasString = true; preserveLiterals = preserveLiterals || t.getPreserveLiterals(); return false; - } - - if (t instanceof LiteralType && !t.isString()) { + } else if (t instanceof NullType) { + hasNull = true; + return true; + } else if (t instanceof LiteralType && !t.isString()) { allStrings = false; } @@ -38,7 +40,7 @@ export class LiteralUnionTypeFormatter implements SubTypeFormatter { if (allStrings && hasString && !preserveLiterals) { return { - type: "string", + type: hasNull ? ["string", "null"] : "string", }; } diff --git a/test/valid-data/string-literals-hack/main.ts b/test/valid-data/string-literals-hack/main.ts index 0e6fc4eab..57489b475 100644 --- a/test/valid-data/string-literals-hack/main.ts +++ b/test/valid-data/string-literals-hack/main.ts @@ -5,6 +5,7 @@ export type MyObject = { stringWithNull: string | null; literalWithNull: "foo" | "bar" | null; literalWithString: "foo" | "bar" | string; + literalWithStringAndNull: "foo" | "bar" | string | null; withRef: "foo" | Union; withRefWithString: Union | string; withHack: "foo" | "bar" | (string & {}); diff --git a/test/valid-data/string-literals-hack/schema.json b/test/valid-data/string-literals-hack/schema.json index 722779f4d..388eceac0 100644 --- a/test/valid-data/string-literals-hack/schema.json +++ b/test/valid-data/string-literals-hack/schema.json @@ -19,6 +19,12 @@ "literalWithString": { "type": "string" }, + "literalWithStringAndNull": { + "type": [ + "string", + "null" + ] + }, "literals": { "enum": [ "foo", @@ -95,6 +101,7 @@ "stringWithNull", "literalWithNull", "literalWithString", + "literalWithStringAndNull", "withRef", "withRefWithString", "withHack", diff --git a/test/vega-lite/schema.json b/test/vega-lite/schema.json index a0a64b449..b5aef98f9 100644 --- a/test/vega-lite/schema.json +++ b/test/vega-lite/schema.json @@ -4428,46 +4428,37 @@ ] }, "BinnedTimeUnit": { - "anyOf": [ - { - "enum": [ - "binnedyear", - "binnedyearquarter", - "binnedyearquartermonth", - "binnedyearmonth", - "binnedyearmonthdate", - "binnedyearmonthdatehours", - "binnedyearmonthdatehoursminutes", - "binnedyearmonthdatehoursminutesseconds", - "binnedyearweek", - "binnedyearweekday", - "binnedyearweekdayhours", - "binnedyearweekdayhoursminutes", - "binnedyearweekdayhoursminutesseconds", - "binnedyeardayofyear" - ], - "type": "string" - }, - { - "enum": [ - "binnedutcyear", - "binnedutcyearquarter", - "binnedutcyearquartermonth", - "binnedutcyearmonth", - "binnedutcyearmonthdate", - "binnedutcyearmonthdatehours", - "binnedutcyearmonthdatehoursminutes", - "binnedutcyearmonthdatehoursminutesseconds", - "binnedutcyearweek", - "binnedutcyearweekday", - "binnedutcyearweekdayhours", - "binnedutcyearweekdayhoursminutes", - "binnedutcyearweekdayhoursminutesseconds", - "binnedutcyeardayofyear" - ], - "type": "string" - } - ] + "enum": [ + "binnedyear", + "binnedyearquarter", + "binnedyearquartermonth", + "binnedyearmonth", + "binnedyearmonthdate", + "binnedyearmonthdatehours", + "binnedyearmonthdatehoursminutes", + "binnedyearmonthdatehoursminutesseconds", + "binnedyearweek", + "binnedyearweekday", + "binnedyearweekdayhours", + "binnedyearweekdayhoursminutes", + "binnedyearweekdayhoursminutesseconds", + "binnedyeardayofyear", + "binnedutcyear", + "binnedutcyearquarter", + "binnedutcyearquartermonth", + "binnedutcyearmonth", + "binnedutcyearmonthdate", + "binnedutcyearmonthdatehours", + "binnedutcyearmonthdatehoursminutes", + "binnedutcyearmonthdatehoursminutesseconds", + "binnedutcyearweek", + "binnedutcyearweekday", + "binnedutcyearweekdayhours", + "binnedutcyearweekdayhoursminutes", + "binnedutcyearweekdayhoursminutesseconds", + "binnedutcyeardayofyear" + ], + "type": "string" }, "Blend": { "enum": [ @@ -19228,29 +19219,9 @@ "type": "object" }, "ParseValue": { - "anyOf": [ - { - "type": "null" - }, - { - "type": "string" - }, - { - "const": "string", - "type": "string" - }, - { - "const": "boolean", - "type": "string" - }, - { - "const": "date", - "type": "string" - }, - { - "const": "number", - "type": "string" - } + "type": [ + "string", + "null" ] }, "PivotTransform": {