I receive a Typescript error that string is not of type 'food' | 'drink' | other, when I do:
order = x
Because Typescript assumes that x can be any string, and not just 'food' | 'drink' | other, so it throws an error.
Of course, I can get rid of this error if I put my code inside the type guard like this:
const order = 'food' // default value
if (x === 'food' || x === 'drink' || x === 'other') {
order = x
}
I just wonder, can I make this code shorter, DRY and non-repetitive?
Just to let you know, putting the strings in an array ['food', 'drink', 'other'] and doing the condition if (array.includes(x)) {order = x}, doesn't do the trick!
xlook like?