Optional
options: Optional
includeNewtypeDeclaration?: booleantoType.Options.includeNewtypeDeclaration
Default: true
toType.Options.preferInterface
Use the preferInterface
option to tell the compiler that you'd prefer the generated
type to be an interface, if possible.
Note: This option has no effect unless you give you name a type via
options.typeName toType.Options.typeName
.
import * as vi from 'vitest'
import * as T from '@sinclair/typebox'
import { box } from '@traversable/typebox'
vi.expect(box.toType(
T.Record(T.Enum({ a: 'A', b: 'B', c: 'C' }), T.Literal(1))
)).toMatchInlineSnapshot
(`"{ A: 1, B: 1, C: 1 }"`)
vi.expect(box.toType(
T.Intersect([T.Number(), T.Union([T.Literal(1), T.Literal(2), T.Literal(3)])])
)).toMatchInlineSnapshot
(`"number & (1 | 2 | 3)"`)
vi.expect(box.toType(
T.Object({
x: T.Optional(T.Array(T.Number())),
y: T.Optional(T.Array(T.Number())),
z: T.Optional(T.Array(T.Number())),
})
)).toMatchInlineSnapshot
(`"{ x?: Array<number>, y?: Array<number>, z?: Array<number> }"`)
// Use the `typeName` option to give you type a name:
vi.expect(box.toType(
T.Object({ a: T.Optional(T.Number()) }),
{ typeName: 'MyType' }
)).toMatchInlineSnapshot
(`"type MyType = { a?: number }"`)
box.toType
Converts an arbitrary typebox schema into a string representing its underlying TypeScript type.