diff --git a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts index b91da60..0b62daf 100644 --- a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts +++ b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts @@ -628,9 +628,9 @@ describe("schemaToTypeAliasDeclaration", () => { { type: "object", properties: { bar: { type: "number" } } }, ], properties: { - foobar: {type: "string"} + foobar: { type: "string" }, }, - required: ['foo', 'foobar'] + required: ["foo", "foobar"], }; expect(printSchema(schema)).toMatchInlineSnapshot(` @@ -641,7 +641,7 @@ describe("schemaToTypeAliasDeclaration", () => { };" `); }); - + it("should combine additionalProperties and allOf", () => { const schema: SchemaObject = { allOf: [ @@ -649,8 +649,8 @@ describe("schemaToTypeAliasDeclaration", () => { { type: "object", properties: { bar: { type: "number" } } }, ], additionalProperties: { - type: "string" - } + type: "string", + }, }; expect(printSchema(schema)).toMatchInlineSnapshot(` @@ -662,7 +662,7 @@ describe("schemaToTypeAliasDeclaration", () => { };" `); }); - + it("should combine properties & additionalProperties & allOf", () => { const schema: SchemaObject = { allOf: [ @@ -670,12 +670,12 @@ describe("schemaToTypeAliasDeclaration", () => { { type: "object", properties: { bar: { type: "number" } } }, ], additionalProperties: { - type: "string" + type: "string", }, properties: { - foobar: {type: "string"} + foobar: { type: "string" }, }, - required: ['bar', 'foobar'] + required: ["bar", "foobar"], }; expect(printSchema(schema)).toMatchInlineSnapshot(` @@ -689,6 +689,23 @@ describe("schemaToTypeAliasDeclaration", () => { `); }); + it("should combine nullable & allOf", () => { + const schema: SchemaObject = { + allOf: [ + { type: "object", properties: { foo: { type: "string" } } }, + { type: "object", properties: { bar: { type: "number" } } }, + ], + nullable: true, + }; + + expect(printSchema(schema)).toMatchInlineSnapshot(` + "export type Test = { + foo?: string; + bar?: number; + } | null;" + `); + }); + it("should combine inline types", () => { const schema: SchemaObject = { allOf: [ diff --git a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts index cef9042..6a68c70 100644 --- a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts +++ b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts @@ -142,10 +142,13 @@ export const getType = ( additionalProperties: schema.additionalProperties }); } - return getAllOf([ - ...schema.allOf, - ...adHocSchemas - ], context); + return f.createUnionTypeNode([ + getAllOf([ + ...schema.allOf, + ...adHocSchemas + ], context), + ...(schema.nullable ? [f.createLiteralTypeNode(f.createNull())] : []), + ]); } if (schema.enum) {