Skip to content

makeEnum

Jason Godesky edited this page Jul 19, 2026 · 2 revisions

makeEnum is a convenience wrapper for makeStringUnionGuard and selectRandomElement that makes it easy for us to instantiate the pattern of string union “enums” that we favor.

Parameters

values: readonly string[]

The array of values to enumerate. This must be declared as const!

Returns

An object with two methods: guard and randomizer.

guard: (candidate: unknown) => candidate is T

A type guard for the type being defined.

randomizer: () => T

A method that returns a randomly-selected option.

Examples

import { makeEnum } from '@revolutionarygamesco/common'

const options = ['a', 'b', 'c'] as const
export type TestOption = typeof options[number]
export const {
  guard: isTestOption,
  randomizer: selectRandomTestOption
} = makeEnum(options)

Clone this wiki locally