I have this array:
// I have this as a const since I need it as a type elsewhere
const fruits = ["strawberry", "banana", "orange", "grapefruit"] as const;
Then I try to check if an unknown value (from like a HTTP controller) is present in the array (to prevent lots of x !== y statements), like below:
const input: string = SomeRandomService.getMyString();
if(!fruits.includes(input)) {
throw new Error("Not present");
}
That results in the following error message on the .includes(input <-- this one):
TS2345: Argument of type 'string' is not assignable to parameter of type '"strawberry" | "banana" | "orange" | "grapefruit"'.
Why does this occur? This seems like valid code to me, because I actually want to do that check to prevent it from not being assignable like TypeScript suggests me to do.
What is the solution to bypass this? (Except doing input as any)