Search


Search something to see results

variant

If you need to use a simple union, checkout union

Type Parameters

Parameters

  • discriminant: keyof T[number]["props"]
  • schemas: T

Returns UnionSchema<TypeOf<T[number]>, InputOf<T[number]>>

import * as x from 'unhoax'

const a = x.object({ type: x.literal('a'), a: x.string }),
const b = x.object({ type: x.literal('b'), b: x.number }),

const schema = x.variant('type', [a, b])
// Schema<{ type: 'a', a: string } | { type: 'b', b: number }>

const result = schema.parse({ type: 'a', a: 'Hello' })
// { success: true, value: { type: 'a', a: 'Hello' } }