From 735c9008d682ad3cff90efbdd89fa5ae83726c1e Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Fri, 2 Jun 2023 18:46:20 +0800 Subject: [PATCH 1/4] fix: broken types with array literal and object entries --- src/generator/dts.ts | 4 ++++ test/types.test.ts | 25 +++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/generator/dts.ts b/src/generator/dts.ts index a397247..2f92c0f 100644 --- a/src/generator/dts.ts +++ b/src/generator/dts.ts @@ -179,6 +179,10 @@ function getTsType( return "any"; } if (Array.isArray(type.type)) { + // if we're mixing objects with literals then we just revert to any array input + if (type.type.length > 1 && type.type.includes("object")) { + return 'any' + } return type.type.map((t) => TYPE_MAP[t]).join("|"); } if (type.type === "array") { diff --git a/test/types.test.ts b/test/types.test.ts index 461da68..f2d8c61 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -21,9 +21,9 @@ describe("resolveSchema", () => { test: { /** * Test - * + * * this is test - * + * * @default \\"test value\\" */ foo: string, @@ -106,13 +106,13 @@ describe("resolveSchema", () => { manual?: Array<{ /** * This is foo prop - * + * */ foo: number, /** * This is bar prop - * + * */ bar?: number, }>, @@ -205,4 +205,21 @@ 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, + }, + }" + `) + }) + }); From f11b9276835c9e4ca165da0a43fcadebd5c91bee Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Fri, 2 Jun 2023 18:52:50 +0800 Subject: [PATCH 2/4] fix: respect object mapping --- src/generator/dts.ts | 12 +++++++----- test/types.test.ts | 5 +++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/generator/dts.ts b/src/generator/dts.ts index 2f92c0f..40a8ac2 100644 --- a/src/generator/dts.ts +++ b/src/generator/dts.ts @@ -179,11 +179,13 @@ function getTsType( return "any"; } if (Array.isArray(type.type)) { - // if we're mixing objects with literals then we just revert to any array input - if (type.type.length > 1 && type.type.includes("object")) { - return 'any' - } - 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 f2d8c61..c28b3ac 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -216,10 +216,11 @@ export interface Untyped { "export interface Untyped { foo: { /** @default [\\"first\\",\\"second\\",{\\"third\\":true}] */ - bar: Array, + bar: Array, }, }" `) }) - }); From d4cba23e18a691e3ec84502922ad0745e5892e90 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 28 Jul 2023 11:40:37 +0200 Subject: [PATCH 3/4] lint --- src/generator/dts.ts | 16 +++++++++------- test/types.test.ts | 14 +++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/generator/dts.ts b/src/generator/dts.ts index 40a8ac2..ecda1dd 100644 --- a/src/generator/dts.ts +++ b/src/generator/dts.ts @@ -179,13 +179,15 @@ function getTsType( return "any"; } if (Array.isArray(type.type)) { - 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("|"); + 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 c28b3ac..60ad271 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -206,12 +206,12 @@ export interface Untyped { `); }); - it('array - literal and object entries', async() => { + it("array - literal and object entries", async () => { const schema = generateTypes( - await resolveSchema(({ - foo: { bar: ['first', 'second', { third: true }] }, - })) - ) + await resolveSchema({ + foo: { bar: ["first", "second", { third: true }] }, + }) + ); expect(schema).toMatchInlineSnapshot(` "export interface Untyped { foo: { @@ -221,6 +221,6 @@ export interface Untyped { }>, }, }" - `) - }) + `); + }); }); From 6c0bca4966473d00128bb41ff5339281cc36ac00 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 28 Jul 2023 11:41:57 +0200 Subject: [PATCH 4/4] update snapshot (it is wrong extra space but unrelared) --- test/types.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/types.test.ts b/test/types.test.ts index 60ad271..57c4ed2 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -21,9 +21,9 @@ describe("resolveSchema", () => { test: { /** * Test - * + * * this is test - * + * * @default \\"test value\\" */ foo: string, @@ -106,13 +106,13 @@ describe("resolveSchema", () => { manual?: Array<{ /** * This is foo prop - * + * */ foo: number, /** * This is bar prop - * + * */ bar?: number, }>,