From 6f1098b71df55e26e0a894a339a2a153a32d10cc Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Fri, 28 Jul 2023 12:42:44 +0300 Subject: [PATCH] fix: broken types with array literal and object entries (#96) --- src/generator/dts.ts | 10 +++++++++- test/types.test.ts | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/generator/dts.ts b/src/generator/dts.ts index a397247..ecda1dd 100644 --- a/src/generator/dts.ts +++ b/src/generator/dts.ts @@ -179,7 +179,15 @@ function getTsType( return "any"; } if (Array.isArray(type.type)) { - return type.type.map((t) => TYPE_MAP[t]).join("|"); + return type.type + .map((t) => { + // object is typed to an empty string by default, we need to type as object + if (t === "object" && type.type.length > 1) { + return `{\n` + _genTypes(type, " ", opts).join("\n") + `\n}`; + } + return TYPE_MAP[t]; + }) + .join("|"); } if (type.type === "array") { return `Array<${getTsType(type.items, opts)}>`; diff --git a/test/types.test.ts b/test/types.test.ts index 461da68..57c4ed2 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -205,4 +205,22 @@ export interface Untyped { }" `); }); + + it("array - literal and object entries", async () => { + const schema = generateTypes( + await resolveSchema({ + foo: { bar: ["first", "second", { third: true }] }, + }) + ); + expect(schema).toMatchInlineSnapshot(` + "export interface Untyped { + foo: { + /** @default [\\"first\\",\\"second\\",{\\"third\\":true}] */ + bar: Array, + }, + }" + `); + }); });