Skip to content

Commit

Permalink
Add tuple support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mause committed Aug 2, 2020
1 parent 40bbc25 commit 04585a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(") & (")})`;
Expand Down
7 changes: 6 additions & 1 deletion src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
tsIntersectionOf,
tsPartial,
tsUnionOf,
tsTupleOf,
} from "./utils";

export const PRIMITIVES: { [key: string]: "boolean" | "string" | "number" } = {
Expand Down Expand Up @@ -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));
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/v3/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -232,6 +239,7 @@ describe("types", () => {
string: string;
array_ref: components['schemas']['array'][];
inferred_array: components['schemas']['array'][];
tuple: [string, number];
nullable: string[] | null;
}
}`)
Expand Down

0 comments on commit 04585a7

Please sign in to comment.