Union
Example
Section titled “Example”import * as p from '@vbudovski/paseri';
const schema = p.union(p.number(), p.string());const data = 'foo';
const result = schema.safeParse(data);if (result.ok) { // result.value typed as `number | string`.}Playground
Section titled “Playground”Discriminated unions
Section titled “Discriminated unions”If every member of a union is an object schema and they share a common literal field, Paseri automatically uses that field as a discriminator.
import * as p from '@vbudovski/paseri';
const schema = p.union( p.object({ type: p.literal('circle'), radius: p.number(), }), p.object({ type: p.literal('square'), side: p.number(), }),);
const result = schema.safeParse({ type: 'circle', radius: 5 });if (result.ok) { // result.value typed as // { type: 'circle'; radius: number } | { type: 'square'; side: number }.}