Skip to content

Array Includes incorrectly uses generic type #53275

Closed
@willyboy

Description

@willyboy

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions