Skip to content

fix(openapi-typescript): prevent type error when items is boolean #2260

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
}
// standard array type
else if (schemaObject.items) {
if ("type" in schemaObject.items && schemaObject.items.type === "array") {
if (hasKey(schemaObject.items, "type") && schemaObject.items.type === "array") {
itemType = ts.factory.createArrayTypeNode(transformSchemaObject(schemaObject.items, options));
} else {
itemType = transformSchemaObject(schemaObject.items, options);
Expand Down Expand Up @@ -579,3 +579,13 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor

return coreObjectType.length ? ts.factory.createTypeLiteralNode(coreObjectType) : undefined;
}

/**
* Check if an object has a key
* @param possibleObject - The object to check
* @param key - The key to check for
* @returns True if the object has the key, false otherwise
*/
function hasKey<K extends string>(possibleObject: unknown, key: K): possibleObject is { [key in K]: unknown } {
return typeof possibleObject === "object" && possibleObject !== null && key in possibleObject;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ describe("transformSchemaObject > array", () => {
// options: DEFAULT_OPTIONS,
},
],
// Prevents: "TypeError: Cannot use 'in' operator to search for 'type' in true"
[
"boolean items",
{
given: { type: "array", items: true },
want: "unknown[]",
},
],
[
"tuple > tuple items",
{
Expand Down