Created
March 28, 2021 14:41
-
-
Save uladkasach/9c14294051e1e7fb632e2a2bc3934b52 to your computer and use it in GitHub Desktop.
isOfEnum.ts
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
// generic fn to create those type guards: https://stackoverflow.com/a/58278753/3068233 | |
const createIsOfEnum = <T>(e: T) => (token: any): token is T[keyof T] => Object.values(e).includes(token as T[keyof T]); | |
// then use it to create a type checker for your particular enums | |
const isPlanet = createIsOfEnum(Planet); | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment