Skip to content

Commit

Permalink
build: fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Sep 3, 2024
1 parent cf787ba commit 4d07ea3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Oas3_1Schema,
Oas3Operation,
Oas3Parameter,
Oas3RequestBody,
Expand All @@ -8,8 +9,10 @@ import {
import { get } from "lodash-es"
import { Context } from "./config"

export type OAS3 = Oas3Schema | Oas3_1Schema

// todo: wrong <T> typing
export const unref = <T extends Oas3RequestBody | Oas3Parameter | Oas3Schema>(
export const unref = <T extends Oas3RequestBody | Oas3Parameter | OAS3>(
ctx: Context,
s?: Referenced<T>,
) => {
Expand Down
8 changes: 4 additions & 4 deletions src/type-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { filterEmpty } from "array-utils-ts"
import { isArray, uniq, upperFirst } from "lodash-es"
import ts from "typescript"
import { Context } from "./config"
import { unref } from "./schema"
import { OAS3, unref } from "./schema"

const f = ts.factory

Expand Down Expand Up @@ -35,7 +35,7 @@ export const normalizeIdentifier = (val: string, asVar = false) => {
return name
}

const makeInlineEnum = (s: Oas3Schema) => {
const makeInlineEnum = (s: OAS3) => {
if (!s.enum) return undefined

const values = filterEmpty(s.enum)
Expand Down Expand Up @@ -68,7 +68,7 @@ const makeInlineEnum = (s: Oas3Schema) => {
return undefined
}

export const makeType = (ctx: Context, s?: Referenced<Oas3Schema>): ts.TypeNode => {
export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {
const mk = makeType.bind(null, ctx)

if (s === undefined) return f.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword)
Expand Down Expand Up @@ -112,7 +112,7 @@ export const makeType = (ctx: Context, s?: Referenced<Oas3Schema>): ts.TypeNode
if ("type" in s) {
// openapi v3.1 can have type as array
if (Array.isArray(s.type)) {
const types: Oas3Schema[] = []
const types: OAS3[] = []
for (const type of s.type) {
if (type === "null") types.push({ type: "null" })
else types.push({ ...s, type })
Expand Down
2 changes: 0 additions & 2 deletions test/type-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ test("type inline", async () => {
t({ oneOf: [{ type: "string" }, { type: "number" }] }, "string | number")
t({ anyOf: [{ type: "string" }, { type: "number" }] }, "string | number")
t({ allOf: [{ type: "string" }, { type: "number" }] }, "string & number")
// @ts-expect-error https://github.com/Redocly/redocly-cli/pull/1362
t({ type: ["string", "number"] }, "string | number")

// arrays of basic types
Expand All @@ -51,7 +50,6 @@ test("type inline", async () => {
t({ type: "array", items: { type: "null" } }, "null[]")
t({ type: "array", items: { type: "object" } }, "object[]") // should be unknown?
t({ type: "array", items: { type: "file" } }, "unknown[]")
// @ts-expect-error https://github.com/Redocly/redocly-cli/pull/1362
t({ type: "array", items: { type: ["string", "number"] } }, "(string | number)[]")

// arrays of arrays
Expand Down

0 comments on commit 4d07ea3

Please sign in to comment.