Skip to content

Instantly share code, notes, and snippets.

@ahiipsa
Created November 26, 2019 14:21
Show Gist options
  • Select an option

  • Save ahiipsa/013c998957ff51df9cd1268696c3a5ba to your computer and use it in GitHub Desktop.

Select an option

Save ahiipsa/013c998957ff51df9cd1268696c3a5ba to your computer and use it in GitHub Desktop.
TypeScript Random Enum
const randomEnum = <T>(arr: readonly T[]) => {
type ReturnType = typeof arr[number];
return (): ReturnType => {
const index = Math.floor(Math.random() * arr.length);
return arr[index];
}
};
const g = randomEnum((<const>['a', 'b']))(); // 'a' | 'b'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment