Skip to content

choice()

Creates a ChoiceFaker<T>. Selects from a fixed set of literal values.

Mixins: Excludable, Nullable, Seedable

Type inference: choice('active', 'inactive') produces ChoiceFaker<'active' | 'inactive'>.

typescript
choice('a', 'b', 'c').create()  // 'b'
MethodDescriptionExample
.weighted(map)Weighted probabilitieschoice('a', 'b').weighted({ a: 0.8 })
.not(...values)Exclude optionschoice('a', 'b', 'c').not('b')

Keys not in the weighted() map share remaining probability equally:

typescript
choice('a', 'b', 'c').weighted({ a: 0.6 })
// a: 60%, b: 20%, c: 20%

← Back to Faker API