Skip to content

Commit d50ea18

Browse files
authored
Add tuple support (#293)
1 parent 3da704c commit d50ea18

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ export function tsArrayOf(type: string): string {
105105
return `(${type})[]`;
106106
}
107107

108+
/** Convert array of types into [T, A, B, ...] */
109+
export function tsTupleOf(types: string[]): string {
110+
return `[${types.join(", ")}]`;
111+
}
112+
108113
/** Convert T, U into T & U; */
109114
export function tsIntersectionOf(types: string[]): string {
110115
return `(${types.join(") & (")})`;

src/v3.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
tsIntersectionOf,
99
tsPartial,
1010
tsUnionOf,
11+
tsTupleOf,
1112
} from "./utils";
1213

1314
export const PRIMITIVES: { [key: string]: "boolean" | "string" | "number" } = {
@@ -92,7 +93,11 @@ export default function generateTypesV3(
9293
]);
9394
}
9495
case "array": {
95-
return tsArrayOf(transform(node.items as any));
96+
if (Array.isArray(node.items)) {
97+
return tsTupleOf(node.items.map(transform));
98+
} else {
99+
return tsArrayOf(transform(node.items as any));
100+
}
96101
}
97102
}
98103

tests/v3/index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ describe("types", () => {
210210
inferred_array: {
211211
items: { $ref: "#/components/schemas/array" },
212212
},
213+
tuple: {
214+
type: "array",
215+
items: [
216+
{ type: "string" },
217+
{ type: "number" }
218+
]
219+
},
213220
nullable: {
214221
type: "array",
215222
items: { type: "string" },
@@ -232,6 +239,7 @@ describe("types", () => {
232239
string: string;
233240
array_ref: components['schemas']['array'][];
234241
inferred_array: components['schemas']['array'][];
242+
tuple: [string, number];
235243
nullable: string[] | null;
236244
}
237245
}`)

0 commit comments

Comments
 (0)