Check is value is member of Union type

“Array first” approach
https://dev.to/hansott/how-to-check-if-string-is-member-of-union-type-1j4m

1
2
3
const ALL_SUITS = ['hearts', 'diamonds', 'spades', 'clubs'] as const
type SuitTuple = typeof ALL_SUITS // type SuitTuple = readonly ["hearts", "diamonds", "spades", "clubs"]
type Suit = SuitTuple[number] // type Suit = "hearts" | "diamonds" | "spades" | "clubs"

Type guard function

1
2
3
function isSuit(value: string): value is Suit {
return ALL_SUITS.includes(value as Suit)
}