enum - nberlette/is GitHub Wiki

@nick/is/enum

isEnum

Signature

function isEnum<T extends EnumLike>(it: T | unknowns): it is Enum<T>;

Check if the given value appears to be a TypeScript enum object, which is a plain object with either all string keys that point to numeric or string values, or string/numeric keys that point to string/numeric values, which are inversely mapped to each other (e.g. A.B === 1 && A[1] === "B").

Parameters
Name Info
it The value to check.
Returns

true if the value appears to be a TypeScript enum object, false otherwise.

Category

Objects

Examples

import { isEnum } from "@nick/is/enum";

enum Foo {
  Bar, // 0
  Baz, // 1
  Qux, // 2
  // implicit reverse mapping:
  // 0: "Bar", 1: "Baz", 2: "Qux",
}

isEnum(Foo); // true

const obj = { Bar: 0, Baz: 1, Qux: 2 };
isEnum(obj); // false

isEnum

Signature

function isEnum<T extends EnumLike = EnumLike>(it: unknown): it is Enum<T>;

isEnum

Signature

function isEnum(it: unknown): it is Enum;

Enum

Signature

export type Enum<T extends EnumLike = EnumLike> = EnumLike & T;

Represents a TypeScript enum object, which is defined as a plain object that satisfies one of the following conditions:

  • String or numeric keys mapped to string or numeric values.
  • Values can be constant or computed.

Declared keys may only be literal static strings. Numeric keys are not allowed in the declaration of an enum. The only legal way to create an enum with numeric keys like "0" is for the compiler to generate them.

String-to-string enums may not have reverse mappings. It is only supported for constant numeric values (whether explicitly specified via initializers, or implicitly assigned by the TS compiler).

When defined with the enum keyword and constant numeric values (or no explicit values specified at all, which defaults to 0 for the first key and increments by 1 for each subsequent key), the TypeScript compiler auto-generates an implicit reverse-mapping from the values back to their respective keys.

Type Parameters
  • T extends EnumLike (default: EnumLike)

Examples

Constant enum definition

enum Abc {
  B = 0,
  C = 1,
}

Mixed-value enum definition (no reverse mapping)

// Mixing string and numeric values in an enum definition will
// prevent the compiler from generating reverse mappings (since
// it only generates such mappings for constant numeric values).
enum Abc {
  B = 0,
  C = "c",
}

Constant enum definition (implicit value assignment)

// auto-generates a reverse mapping from 0 => B and 1 => C
enum Abc {
  B, // = 0
  C, // = 1
  // "0" = "B",
  // "1" = "C",
}

Computed enum definition

enum C {
  D = "e" + "f", // "ef"
}

EnumLike

Represents an "enum-like" object, which is a plain object composed of either all string keys and string values, or a two-way mapping of string keys to numeric values (and vice versa).

This is a supertype of the type of object created by TypeScript's builtin enum keyword. Its primary consumer is the isEnum function.

Category

Types

Tags

enum objects

Examples

// when we create an enum with constant numeric values, the TypeScript
// compiler auto-generates an implicit reverse-mapping from the values
// back to their respective keys, providing us with the capability to
// access the keys by their values.
enum Priority {
  Low = 0x0,
  Medium = 0x1,
  High = 0x2,
  Insane = 0xFF,
}

// the actual object generated by the enum syntax above looks like this:
const Priority = {
  Low: 0,
  Medium: 1,
  High: 2,
  Insane: 255,
  "0": "Low",
  "1": "Medium",
  "2": "High",
  "255": "Insane",
} satisfies EnumLike;

// this provides us with the ability to access a key even when we only have
// its value, a language feature that is popular in other OOP languages such
// as C#, Java, and C++:

console.log(Priority.High); // 2
console.log(Priority[2]); // "High"
console.assert(Priority.High === Priority[Priority[2]]);
console.assert(Priority[2] === Priority[Priority.High]);
Type Parameters
  • K extends string | number (default: string | number)
  • V extends string | number (default: string | number)

Index Signatures

EnumLike

readonly [key: string]:  K | V | undefined

EnumLike

readonly [index: number]: K