Skip to main content

Overview

reatomEnum creates an atom that manages enumerated (enum) state with automatically generated setter methods for each variant. It supports both camelCase and snake_case naming conventions and provides compile-time type safety.

Import

Type Signature

Parameters

ReadonlyArray<T>
required
An array of string literals representing the possible enum values
string | EnumAtomOptions<T, Format>
Either a string name for the atom, or an options object

EnumAtomOptions

string
default:"'enumAtom'"
Optional name for the atom, useful for debugging
'camelCase' | 'snake_case'
default:"'camelCase'"
The naming format for generated setter methods
T
default:"variants[0]"
The initial enum value. Defaults to the first variant

Properties

enum

A static object mapping each variant to itself, useful for comparisons and type guards. Type: { [K in T]: K }

Methods

Dynamic Setters (camelCase)

For each variant, a setter method is generated with the pattern set{CapitalizedVariant}.

Dynamic Setters (snake_case)

When using format: 'snake_case', setter methods follow the pattern set_{variant}.

reset

Resets the enum to its initial value. Returns: T - The initial value

Basic Usage

Advanced Usage

Snake Case Format

Custom Initial State

Theme Switcher

View Mode

Status Machine

Type Guards with Enum

Validation

Using with Atom Methods

Since EnumAtom extends Atom, you can use standard atom methods:

Notes

  • The enum atom validates all set operations and throws a ReatomError if an invalid value is provided
  • Setter methods are automatically generated based on the format option
  • The first variant is used as the initial state if no initState is provided
  • The enum property provides a convenient way to reference variants without string literals
  • Type safety is preserved throughout, preventing invalid enum values at compile time
  • All variants must be provided as a readonly array of string literals for proper type inference