Skip to content

Instantly share code, notes, and snippets.

@ieatfood
Created July 12, 2022 02:03
Show Gist options
  • Save ieatfood/2b3318b2aad28c19177d0ed9e10ec4b7 to your computer and use it in GitHub Desktop.
Save ieatfood/2b3318b2aad28c19177d0ed9e10ec4b7 to your computer and use it in GitHub Desktop.
/**
* Returns an array of the specified enum type with values that are the
* intersection of the array and enum type.
*
* Example:
* convertArrayToEnumType(["VISA", "CARTES_BANCAIRES"], AllowedCardNetworks)
* => ["VISA"]
*/
export function convertArrayToEnumType<S, T extends string>(arr: Array<S>, type: Record<T, T>): Array<T> {
return arr.filter((k) => Object.keys(type).includes((k as unknown) as T)).map((k) => (k as unknown) as T);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment