I have a typed array but typescript only lets me use the Array.includes() function with values that are included in the array, doesn’t that mean it will never return false?
type Pet = 'cat' | 'dog' | 'fish' | 'hamster';
const pets: Pet[] = ['cat', 'dog', 'fish', 'hamster'];
const someInput1: string = 'fish';
const someInput2: string = 'snake';
pets.includes('cat'); // true
pets.includes(someInput1); // Argument of type 'string' is not assignable to parameter of type 'Pet'.ts(2345)
pets.includes(someInput2); // Argument of type 'string' is not assignable to parameter of type 'Pet'.ts(2345)
I know that someInput2 could be a value that is not included in the array, thats the reason I am calling that function, because I want to know if someInput2 is included in the array or not
I have found a lot of ways to hide this error, like
(pets as string[]).includes(someInput2); // false
pets.includes(someInput2 as any); // false
pets.some((value) => value === someInput2) // false
const allPets: string[] = pets;
allPets.includes(someInput2); // false
But my question is not about how to work around this, but to know if this a typescript bug? if not, what is the indented way to use Array.includes()