API reference@evolu/commonType › union

Call Signature

function union<Members>(...members: Members): UnionType<Members>;

Defined in: packages/common/src/Type.ts:3575

Union Type.

UnionType represents a union of multiple member Types. Accepts both Type and literal values as arguments.

Note that the union Type Factory delegates fromParent to fromUnknown. That's because the union members can have different Parent types, and at runtime, it is impossible to determine which member should process a given Parent value.

Example

const AorB = union("a", "b");
const result1 = AorB.from("a"); // ok("a")
const result2 = AorB.from("c"); // err

const StringOrNumber = union(String, Number);
const result3 = StringOrNumber.from(42); // ok(42)

Call Signature

function union<Literals>(
  ...literals: Literals
): UnionType<{ [K in string | number | symbol]: LiteralType<Literals[K]> }>;

Defined in: packages/common/src/Type.ts:3580

With literal values.