Skip to content

Instantly share code, notes, and snippets.

@nixjs
Created April 26, 2023 14:13
Show Gist options
  • Save nixjs/db2cd7db64861a801dbae766f224621e to your computer and use it in GitHub Desktop.
Save nixjs/db2cd7db64861a801dbae766f224621e to your computer and use it in GitHub Desktop.
Convert array of strings into enum
type ValuesOfArray<T extends ReadonlyArray<any>> = T[number]
type ToObj<K extends string> = {
[P in K]: P
}
export const generate = <T extends readonly any[], K extends ValuesOfArray<T>>(
arr: T,
start?: number,
convention?: number
): Readonly<ToObj<K>> => {
return arr.reduce((acc, elem, idx) => {
return {
...acc,
[elem]: idx + (start || 0) + (convention || 0)
}
}, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment