Skip to content

Instantly share code, notes, and snippets.

@MikelArnaiz
Last active June 5, 2020 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikelArnaiz/e64e7d128a2b1b1fe25e2f56e2502350 to your computer and use it in GitHub Desktop.
Save MikelArnaiz/e64e7d128a2b1b1fe25e2f56e2502350 to your computer and use it in GitHub Desktop.
Ensure getting an array with all items of a TypeScript type
/**
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