Je TypeScript tak obmedzujúci pri Array.includes(), že nikdy nemôže vrátiť false?
Mám typizované pole, ale TypeScript mi umožňuje používať funkciu Array.includes() iba s hodnotami, ktoré sú zahrnuté v poli, znamená to teda, že nikdy nevráti 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)
Viem, že someInput2 môže byť hodnota, ktorá nie je zahrnutá v poli, preto volám túto funkciu, pretože chcem vedieť, či je someInput2 zahrnutá v poli alebo nie
Našiel som veľa spôsobov, ako túto chybu skryť, napríklad
(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
Ale moja otázka nie je o tom, ako toto obísť, ale či ide o chybu TypeScriptu? Ak nie, aký je zamýšľaný spôsob použitia Array.includes()