Enum Utilities

Utility functions to work with TypeScript enums, such as getting all keys or values.

TypeScript
david

13 weeks ago

enum Colors {
  Red = "RED",
  Green = "GREEN",
  Blue = "BLUE",
}

function getEnumKeys<E>(e: E): (keyof E)[] {
  return Object.keys(e).filter((k) => isNaN(Number(k))) as (keyof E)[];
}

function getEnumValues<E>(e: E): E[keyof E][] {
  return Object.values(e).filter((v) => typeof v === "string") as E[keyof E][];
}
0