Skip to content

Instantly share code, notes, and snippets.

@uladkasach
Created March 28, 2021 14:41
Show Gist options
  • Save uladkasach/9c14294051e1e7fb632e2a2bc3934b52 to your computer and use it in GitHub Desktop.
Save uladkasach/9c14294051e1e7fb632e2a2bc3934b52 to your computer and use it in GitHub Desktop.
isOfEnum.ts
// 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