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(...). Use .uniform for equal odds, or .weighted with [weight, member] pairs. Each weight is divided by the total of all weights. In the example above, "USD" comes up 60% of the time, "YEN" 30%, and "GBP" 10%. A weight of 0 turns that member off, and the others are still drawn. It throws if every weight is 0. Negative weights, NaN, and Infinity are invalid.

The members don't have to be written inline. An array built at runtime, such as records.map((r) => r.id), works too. That is how a child schema refers to a pool of parents you already fabricated. See Referencing a pool. Its type keeps the union of the members but not how many there are. Since it can't be checked for emptiness at compile time, an empty array throws when the schema is built.

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

For members that are each drawn at most once, see T.unique.enum. To pick among schemas rather than values, see T.choice.