Skip to content

Commit

Permalink
fix: broken types with array literal and object entries (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw authored Jul 28, 2023
1 parent 3b91d06 commit 6f1098b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/generator/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}>`;
Expand Down
18 changes: 18 additions & 0 deletions test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|{
[key: string]: any
}>,
},
}"
`);
});
});

0 comments on commit 6f1098b

Please sign in to comment.