diff --git a/src/utils.ts b/src/utils.ts index abe45f430..e6467e51d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -105,6 +105,11 @@ export function tsArrayOf(type: string): string { return `(${type})[]`; } +/** Convert array of types into [T, A, B, ...] */ +export function tsTupleOf(types: string[]): string { + return `[${types.join(", ")}]`; +} + /** Convert T, U into T & U; */ export function tsIntersectionOf(types: string[]): string { return `(${types.join(") & (")})`; diff --git a/src/v3.ts b/src/v3.ts index ce9bbe12a..2db8ccf1d 100644 --- a/src/v3.ts +++ b/src/v3.ts @@ -8,6 +8,7 @@ import { tsIntersectionOf, tsPartial, tsUnionOf, + tsTupleOf, } from "./utils"; export const PRIMITIVES: { [key: string]: "boolean" | "string" | "number" } = { @@ -84,7 +85,11 @@ export default function generateTypesV3( ]); } case "array": { - return tsArrayOf(transform(node.items as any)); + if (Array.isArray(node.items)) { + return tsTupleOf(node.items.map(transform)); + } else { + return tsArrayOf(transform(node.items as any)); + } } } diff --git a/tests/v3/index.test.ts b/tests/v3/index.test.ts index 7de9bdc3d..e248badd9 100644 --- a/tests/v3/index.test.ts +++ b/tests/v3/index.test.ts @@ -210,6 +210,13 @@ describe("types", () => { inferred_array: { items: { $ref: "#/components/schemas/array" }, }, + tuple: { + type: "array", + items: [ + { type: "string" }, + { type: "number" } + ] + }, nullable: { type: "array", items: { type: "string" }, @@ -232,6 +239,7 @@ describe("types", () => { string: string; array_ref: components['schemas']['array'][]; inferred_array: components['schemas']['array'][]; + tuple: [string, number]; nullable: string[] | null; } }`)