Type alias LabeledEnum<T>

LabeledEnum<T>: {
    accessor: T;
    values: (() => T[keyof T][]);
    hasValue: ((value) => value is T[keyof T]);
    assertValue: ((value) => asserts value is T[keyof T]);
    keys: (() => (keyof T)[]);
    hasKey: ((key) => key is keyof T);
    assertKey: ((key) => asserts key is keyof T);
    entries: (() => [keyof T, T[keyof T]][]);
    hasEntry: ((entry) => entry is [keyof T, T[keyof T]]);
    assertEntry: ((entry) => asserts entry is [keyof T, T[keyof T]]);
    keyOf: (<V>(value) => Invert<T>[V]);
}

Enum object created from a map of key-value pairs.

A labeled enum is more similar to a built-in enum, because it distinguishes between keys and values.

A labeled enum supports the same methods as a SimpleEnum, as well as additional methods for handling keys and key-value pairs.

Type Parameters

  • T extends EnumSourceObject

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

Type declaration

  • accessor: T

    Object mapping keys to values (mutations prevented with Object.freeze). Can be used for accessing values by keys using dot syntax.

    Example

    const LOCALES = Enum({ English: 'en', Czech: 'cs', Slovak: 'sk' });
    const Locale = LOCALES.accessor;

    const locale = Locale.Czech; // locale is 'cs'
  • values: (() => T[keyof T][])
      • (): T[keyof T][]
      • Lists all values.

        Returns T[keyof T][]

        Array of values (safe to mutate).

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

        Parameters

        • value: unknown

          Value with unknown type.

        Returns value is T[keyof T]

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

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

        Parameters

        • value: unknown

          Value with unknown type.

        Returns asserts value is T[keyof T]

        Nothing if valid enum value, throws error if invalid.

        Throws

        RangeError

  • keys: (() => (keyof T)[])
      • (): (keyof T)[]
      • Lists all keys.

        Returns (keyof T)[]

        Array of keys (similar to Object.keys).

  • hasKey: ((key) => key is keyof T)
      • (key): key is keyof T
      • Checks if key is contained in enum's keys.

        Parameters

        • key: unknown

          Value with unknown type.

        Returns key is keyof T

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

  • assertKey: ((key) => asserts key is keyof T)
      • (key): asserts key is keyof T
      • Checks if key is contained in enum's keys.

        Parameters

        • key: unknown

          Value with unknown type.

        Returns asserts key is keyof T

        Nothing if valid enum key, throws error if invalid.

        Throws

        RangeError

  • entries: (() => [keyof T, T[keyof T]][])
      • (): [keyof T, T[keyof T]][]
      • Lists all key-value pairs.

        Returns [keyof T, T[keyof T]][]

        Array of tuples (similar to Object.entries).

  • hasEntry: ((entry) => entry is [keyof T, T[keyof T]])
      • (entry): entry is [keyof T, T[keyof T]]
      • Checks if key-value pair is contained in enum.

        Parameters

        • entry: unknown

          Value with unknown type.

        Returns entry is [keyof T, T[keyof T]]

        Nothing if tuple with matching key and value, throws error if invalid.

  • assertEntry: ((entry) => asserts entry is [keyof T, T[keyof T]])
      • (entry): asserts entry is [keyof T, T[keyof T]]
      • Checks if key-value pair is contained in enum.

        Parameters

        • entry: unknown

          Value with unknown type.

        Returns asserts entry is [keyof T, T[keyof T]]

        Boolean (true if tuple with matching key and value, otherwise false).

        Throws

        RangeError

  • keyOf: (<V>(value) => Invert<T>[V])
      • <V>(value): Invert<T>[V]
      • Access key for given value.

        Type Parameters

        Parameters

        • value: V

          Enum value.

        Returns Invert<T>[V]

        Enum key matching given value.

Generated using TypeDoc