API reference / @evolu/common / Type / union
Function: union()
Defined in: packages/common/src/Type.ts:2990
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
| Parameter | Type |
|---|---|
...members | Members |
Returns
UnionType<Members>
Defined in: packages/common/src/Type.ts:2994
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
| Parameter | Type |
|---|---|
...literals | Literals |
Returns
UnionType<{ [K in string | number | symbol]: LiteralType<Literals[K<K>]> }>