Skip to content

Commit b3e9c5d

Browse files
committed
fix(openapi-typescript): prevent type error when items is boolean
1 parent 1f6c0e8 commit b3e9c5d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/openapi-typescript/src/transform/schema-object.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
322322
}
323323
// standard array type
324324
else if (schemaObject.items) {
325-
if ("type" in schemaObject.items && schemaObject.items.type === "array") {
325+
if (hasKey(schemaObject.items, "type") && schemaObject.items.type === "array") {
326326
itemType = ts.factory.createArrayTypeNode(transformSchemaObject(schemaObject.items, options));
327327
} else {
328328
itemType = transformSchemaObject(schemaObject.items, options);
@@ -579,3 +579,13 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
579579

580580
return coreObjectType.length ? ts.factory.createTypeLiteralNode(coreObjectType) : undefined;
581581
}
582+
583+
/**
584+
* Check if an object has a key
585+
* @param possibleObject - The object to check
586+
* @param key - The key to check for
587+
* @returns True if the object has the key, false otherwise
588+
*/
589+
function hasKey<K extends string>(possibleObject: unknown, key: K): possibleObject is { [key in K]: unknown } {
590+
return typeof possibleObject === 'object' && possibleObject !== null && key in possibleObject;
591+
}

packages/openapi-typescript/test/transform/schema-object/array.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ describe("transformSchemaObject > array", () => {
1818
// options: DEFAULT_OPTIONS,
1919
},
2020
],
21+
// Prevents: "TypeError: Cannot use 'in' operator to search for 'type' in true"
22+
[
23+
"boolean items",
24+
{
25+
given: { type: "array", items: true },
26+
want: "unknown[]",
27+
},
28+
],
2129
[
2230
"tuple > tuple items",
2331
{

0 commit comments

Comments
 (0)