API Reference / @evolu/common / Type / union

Function: union()

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

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)

Type Parameters

Type Parameter
Members extends [AnyType, AnyType, ...AnyType[]]

Parameters

ParameterType
...membersMembers

Returns

UnionType<Members>

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

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)

Type Parameters

Type Parameter
Literals extends [Literal, Literal, ...Literal[]]

Parameters

ParameterType
...literalsLiterals

Returns

UnionType<{ [K in string | number | symbol]: LiteralType<Literals[K<K>]> }>

Was this page helpful?