Interface IEnum

Core Enum object.

  • Calling it directly (Enum(...)) creates an enum.
  • Also exposes methods for deriving enums from other enums:
    • either by adding more values (Enum.extend(...)),
    • or by removing values (Enum.exclude(...)).

Each function has different overloads to support creating both simple and labeled enums.

Hierarchy

  • IEnum
  • Creates a simple enum from an array of values.

    Type Parameters

    • T extends EnumPrimitive

      Type of values (union of strings or numbers).

    Parameters

    • values: T[]

      Array of all values.

    Returns SimpleEnum<T>

    Simple enum object.

    Example

    const ROLES = Enum(['viewer', 'editor', 'owner']);
    
  • Creates a labeled enum from an object.

    Type Parameters

    • const T extends EnumSourceObject

      Object type (defining types of keys and correspoding values).

    Parameters

    • obj: T

      Object mapping keys to values.

    Returns LabeledEnum<T>

    Labeled enum object.

    Example

    const LANGUAGES = Enum({
    English: 'English',
    Czech: 'Čeština',
    Slovak: 'Slovenčina',
    });

Methods

Methods

  • Creates a simple enum by adding values to another enum.

    Type Parameters

    • TEnum extends AnySimpleEnum

      Type of simple enum or labeled enum.

    • TExtra extends EnumPrimitive

      Type of added values (union of strings or numbers).

    Parameters

    • srcEnum: TEnum

      Source enum object (simple or labeled).

    • extraValues: TExtra[]

      Values to be added.

    Returns SimpleEnumExtended<TEnum, TExtra>

    Simple enum object.

    Example

    const STATUSES = Enum(['alive', 'dead']);
    const INFECTED_STATUSES = Enum.extend(STATUSES, ['zombie']);
  • Creates a labeled enum by adding keys and values to another enum.

    Type Parameters

    • TEnum extends AnyLabeledEnum

      Type of labeled enum.

    • const TExtra extends EnumSourceObject

      Object type (defining types of added keys and correspoding values).

    Parameters

    • srcEnum: TEnum

      Source labeled enum object.

    • extraObj: TExtra

      Object with key-value pairs to be added.

    Returns LabeledEnumExtended<TEnum, TExtra>

    Labeled enum object.

    Example

    const LOCALES = Enum({ English: 'en', Czech: 'cs', Slovak: 'sk' });
    const EXTENDED_LOCALES = Enum.extend(LOCALES, { Spanish: 'es' });
  • Creates a labeled enum by removing keys from another enum.

    Type Parameters

    Parameters

    • srcEnum: TEnum

      Source labeled enum object.

    • keys: TKey[]

      Array of keys to remove.

    Returns LabeledEnumExcludedByKeys<TEnum, TKey>

    Labeled enum object.

    Example

    const LEVELS = Enum({ off: 0, warn: 1, error: 2 });
    const ERROR_LEVELS = Enum.exclude(LEVELS, ['off']);
  • Creates a labeled enum by removing values from another enum.

    Type Parameters

    Parameters

    • srcEnum: TEnum

      Source labeled enum object.

    • values: TValue[]

      Array of values to remove.

    Returns LabeledEnumExcludedByValues<TEnum, TValue>

    Labeled enum object.

    Example

    const LEVELS = Enum({ off: 0, warn: 1, error: 2 });
    const ERROR_LEVELS = Enum.exclude(LEVELS, [0]);
  • Creates a simple enum by removing values from another enum.

    Type Parameters

    Parameters

    • srcEnum: TEnum

      Source simple enum object.

    • values: TValue[]

      Array of values to remove.

    Returns SimpleEnumExcluded<TEnum, TValue>

    Simple enum object.

    Example

    const STATUSES = Enum(['pending', 'fulfilled', 'rejected']);
    const SETTLED_STATUSES = Enum.exclude(STATUSES, ['pending']);

Generated using TypeDoc