Created
April 26, 2023 14:13
-
-
Save nixjs/db2cd7db64861a801dbae766f224621e to your computer and use it in GitHub Desktop.
Convert array of strings into enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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