Skip to content

Instantly share code, notes, and snippets.

@jasonbyrne
Created November 29, 2022 05:42
Show Gist options
  • Save jasonbyrne/75ea3734650e8e464499cb7c175af2fc to your computer and use it in GitHub Desktop.
Save jasonbyrne/75ea3734650e8e464499cb7c175af2fc to your computer and use it in GitHub Desktop.
const values = ['foo', 'bar'] as const;
type MyValues = typeof values[number];
function isOneOfMyValues(elem: unknown): elem is MyValues {
const opts: string[] = [...values];
return opts.includes(String(elem));
}
function lengthOfMyThing(x: MyValues | null) {
if (isOneOfMyValues(x)) {
return x.length;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment