Created
November 29, 2022 05:42
-
-
Save jasonbyrne/75ea3734650e8e464499cb7c175af2fc to your computer and use it in GitHub Desktop.
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
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