Skip to content

Instantly share code, notes, and snippets.

@jasonbyrne
Created November 29, 2022 05:42
Embed
What would you like to do?
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