TypeScript Enum to keyof Enum Array

PHOTO EMBED

Wed Apr 21 2021 16:17:11 GMT+0000 (UTC)

Saved by @Etzeo #typescript

enum Colors {
  'black',
  'brown',
  'red',
  'orange',
  'yellow',
  'green',
  'blue',
  'violet',
  'grey',
  'white'
}

function EnumKeys<T>(enumType: T): (keyof T)[] {
  return (Object.keys(enumType) as Array<keyof T>)
      .filter(value => isNaN(Number(value)) !== false);
}

export const COLORS = EnumKeys(Colors)
// COLORS = ['black','brown','red','orange','yellow','green','blue','violet','grey','white']
content_copyCOPY

Pass in Enum and get the Keys returned in an Array

https://exercism.io/tracks/typescript/exercises/resistor-color/solutions/31443ac1129c4bcfbcb161e931bdbe36