diff --git a/packages/quicktype-core/src/language/TypeScriptZod/TypeScriptZodRenderer.ts b/packages/quicktype-core/src/language/TypeScriptZod/TypeScriptZodRenderer.ts index 6406101476..580d65e364 100644 --- a/packages/quicktype-core/src/language/TypeScriptZod/TypeScriptZodRenderer.ts +++ b/packages/quicktype-core/src/language/TypeScriptZod/TypeScriptZodRenderer.ts @@ -124,9 +124,14 @@ export class TypeScriptZodRenderer extends ConvenienceRenderer { ], (_enumType) => panic("Should already be handled."), (unionType) => { - const children = Array.from(unionType.getChildren()).map( - (type: Type) => this.typeMapTypeFor(type, false), - ); + const children = Array.from(unionType.getChildren()) + // Coercing schemas can accept null, so handle it first. + .sort( + (a, b) => + Number(b.kind === "null") - + Number(a.kind === "null"), + ) + .map((type: Type) => this.typeMapTypeFor(type, false)); return ["z.union([", ...arrayIntercalate(", ", children), "])"]; }, (_transformedStringType) => { diff --git a/test/languages.ts b/test/languages.ts index 6bcfd43611..8e5da86de3 100644 --- a/test/languages.ts +++ b/test/languages.ts @@ -1879,9 +1879,6 @@ export const TypeScriptZodLanguage: Language = { // Does not handle top level array "bug863.json", - // z.coerce.date() coerces null to the Unix epoch: #2880 - "bug2590.json", - "no-classes.json", "00c36.json", "10be4.json",