Skip to content
Logo

T.enum

A literal: a random member from a set of values.

T.enum.uniform(["electronics", "clothing", "food"]);
 
T.enum.weighted([
  [6, "USD"],
  [3, "YEN"],
  [1, "GBP"],
]);

There's no bare T.enum(...) — pick .uniform (equal probability) or .weighted (relative probability via [weight, member] pairs). Weights are relative between members: each value can be read as the numerator over the sum of all values. In the example above, "YEN" is 3× more likely than "GBP"; "USD" is 2× more likely than "YEN"; "USD" is 6× more likely than "GBP". A weight of 0 disables that member (the remaining members still draw). Every weight zero throws; negative weights, NaN, and Infinity are invalid.

The members don't have to be written inline — an array built at runtime (e.g. records.map((r) => r.id)) works too, which is how a child schema references a pool of already-fabricated parents — see Referencing a pool. Its type keeps the union of the members but not how many there are, and because it can't be checked for emptiness at compile time, an empty one throws when the schema is built.

Like T.always, an enum's members can be any value, not just primitives.

To pick among schemas rather than values, see T.choice.