Type alias SimpleEnum<T>

SimpleEnum<T>: {
    accessor: {
        readonly [K in T]: K
    };
    values: (() => T[]);
    hasValue: ((value) => value is T);
    assertValue: ((value) => asserts value is T);
}

Enum object created from a list of values.

A simple enum is more similar to a union than a built-in enum. Unlike a LabeledEnum, no keys are defined for accessing values.

Type Parameters

  • T extends EnumPrimitive

    Type of value (union of strings or numbers).

Type declaration

  • accessor: {
        readonly [K in T]: K
    }

    Object mapping keys to values (mutations prevented with Object.freeze). Can be used for accessing values by keys using dot syntax. Unlike with labeled enums, a key and its value will always be exactly the same.

    Example

    const STATUSES = Enum(['pending', 'fulfilled', 'rejected']);
    const Status = STATUSES.accessor;

    const status = Status.pending; // status is 'pending'
  • values: (() => T[])
      • (): T[]
      • Lists all values.

        Returns T[]

        Array of values (safe to mutate).

  • hasValue: ((value) => value is T)
      • (value): value is T
      • Checks if value is contained in enum's set of values.

        Parameters

        • value: unknown

          Value with unknown type.

        Returns value is T

        Boolean (true if valid enum value, otherwise false).

  • assertValue: ((value) => asserts value is T)
      • (value): asserts value is T
      • Checks if value is contained in enum's set of values.

        Parameters

        • value: unknown

          Value with unknown type.

        Returns asserts value is T

        Nothing if valid enum value, throws error if invalid.

        Throws

        RangeError

Generated using TypeDoc