@traversable/schema
    Preparing search index...

    Function toType

    • Converts an arbitrary zod schema into a string representing its underlying TypeScript type.

      Parameters

      Returns string

      import * as vi from "vitest"
      import { z } from 'zod'
      import { zx } from "@traversable/zod"

      vi.expect(zx.toType(
      z.record(z.enum(['a', 'b', 'c']), z.set(z.string()))
      )).toMatchInlineSnapshot
      (`"Record<"a" | "b" | "c", Set<string>>"`)

      vi.expect(zx.toType(
      z.object({ a: z.optional(z.string()), b: z.number() }).catchall(z.boolean())
      )).toMatchInlineSnapshot
      (`"{ a?: string, b: number } & { [x: string]: boolean }"`)

      vi.expect(zx.toType(
      z.tuple([z.number()]).rest(z.boolean())
      )).toMatchInlineSnapshot
      (`"[number, ...boolean[]]"`)

      // Use the `typeName` option to give you type a name:
      vi.expect(zx.toType(
      z.object({ a: z.optional(z.number()) }),
      { typeName: 'MyType' }
      )).toMatchInlineSnapshot
      (`"type MyType = { a?: number }"`)
    • Converts an arbitrary zod schema into a string representing its underlying TypeScript type.

      Type Parameters

      • T

      Parameters

      Returns string

      import * as vi from "vitest"
      import { z } from 'zod'
      import { zx } from "@traversable/zod"

      vi.expect(zx.toType(
      z.record(z.enum(['a', 'b', 'c']), z.set(z.string()))
      )).toMatchInlineSnapshot
      (`"Record<"a" | "b" | "c", Set<string>>"`)

      vi.expect(zx.toType(
      z.object({ a: z.optional(z.string()), b: z.number() }).catchall(z.boolean())
      )).toMatchInlineSnapshot
      (`"{ a?: string, b: number } & { [x: string]: boolean }"`)

      vi.expect(zx.toType(
      z.tuple([z.number()]).rest(z.boolean())
      )).toMatchInlineSnapshot
      (`"[number, ...boolean[]]"`)

      // Use the `typeName` option to give you type a name:
      vi.expect(zx.toType(
      z.object({ a: z.optional(z.number()) }),
      { typeName: 'MyType' }
      )).toMatchInlineSnapshot
      (`"type MyType = { a?: number }"`)