Closed
Description
Bug Report
Array.includes should not be inferring type. Especially when the type is a string and the array is defined by a union of strings.
The point of using includes is to see if something exists in an array. If the thing we are looking for is a string in an array with a union type of strings, we get an error like, "argument of type string is not assignable to parameter of type 'a' | 'b'".
🔎 Search Terms
array, includes
🕗 Version & Regression Information
I'm on 4.5.5
⏯ Playground Link
Playground link with relevant code
💻 Code
let thing: string = 'random string that is not hardcoded';
const allowedStrings: ('a' | 'b') = ['a', 'b']
if (!allowedStrings.includes(thing)) {
// do something
}
🙁 Actual behavior
An error is thrown preventing us from using includes to see if our string is in the array.
🙂 Expected behavior
No error is thrown as we don't know if the string is in the array or not until at runtime.