Last active
June 5, 2020 15:45
-
-
Save MikelArnaiz/e64e7d128a2b1b1fe25e2f56e2502350 to your computer and use it in GitHub Desktop.
Ensure getting an array with all items of a TypeScript type
This file contains 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
/** | |
This util is a trick to ensure you get an array with all the elements of a type. | |
You have to pass an object with all the possible types as keys, and true as a value (arbitrarily chosen to be true) | |
*/ | |
export const getListOfType = <A extends string>(obj: Record<A, true>): A[] => { | |
return keys(obj); | |
}; | |
function keys<O extends object>(o: O): Array<keyof O> { | |
return Object.keys(o) as Array<keyof O>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment