Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: broken types with array literal and object entries #96

Merged
merged 5 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}>,
},
}"
`);
});
});