Skip to content

Array.includes() should accept any arg that extends the array's type #60073

Closed

Description

@erikdstock

🔎 Search Terms

array.includes, types

🕗 Version & Regression Information

  • This is the behavior in every version I tried - the type definition is 7 years old.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAtgTwIICcUEMEC4bRQSzAHMBtAXRgF4YSAidWgGhloCNayBYAKB6gQAOAUxgBZTKyEA5AK4AbOVVxQCxGAB8YYeXJ6hIsASiEQhYKADV0cmUOr1aMdBDETpOveGjx8ECISIrGxF7YEcnF3EESVkFTwMtHSDbJW0FCNdo9zjebn1vEgQTZjAQZmh8BSkQCmoSHhhG+GQ0TAA6QmAbABMTAAojEzNLa1sASkYGpsRUDAQOsC6ZXog+uF9-YmShCanGmdb5zp7+tLltsZ4yIA

💻 Code

No response

🙁 Actual behavior

I should not receive type errors because an argument of type string | null might be included in an array of type string[]. It is the job of the method to make the final determination.

🙂 Expected behavior

I get type errors because Array<string>.includes() will only accept a string value. I have to cast my argument to a string in order to perform the check.

Additional information about the issue

Relevant code.

Basic suggested change:

interface Array<T> {
    /**
     * Determines whether an array includes a certain element, returning true or false as appropriate.
     * @param searchElement The element to search for.
     * @param fromIndex The position in this array at which to begin searching for searchElement.
     */
-    includes(searchElement: T, fromIndex?: number): boolean;
+    includes<U extends T>(searchElement: U, fromIndex?: number): boolean;}

interface ReadonlyArray<T> {
    /**
     * Determines whether an array includes a certain element, returning true or false as appropriate.
     * @param searchElement The element to search for.
     * @param fromIndex The position in this array at which to begin searching for searchElement.
     */
-    includes(searchElement: T, fromIndex?: number): boolean;
+    includes<U extends T>(searchElement: U, fromIndex?: number): boolean;}
// ...
}

Activity

  1. jcalz commented on Sep 26, 2024

    @jcalz
    Contributor

    That change wouldn’t help; it already accepts subtypes of the array element. What you want is the opposite (since string extends (string | null) and not vice versa). This is a duplicate of #26255 . Ultimately you’re looking for #14520

  2. erikdstock commented on Sep 26, 2024

    @erikdstock
    Author

    That change wouldn’t help; it already accepts subtypes of the array element. What you want is the opposite (since string extends (string | null) and not vice versa). This is a duplicate of #26255 . Ultimately you’re looking for #14520

    Thanks for the link. I see there is a lot of discussion here - I searched but apparently missed it. In use case it seemed wrong to have to cast in this relatively straightforward["a", "b"].includes(stringOrNull as string). If this has been decided years ago and I'm not contributing anything new feel free to close.

    WIP PR was here (first time contributing so I may have been doing it wrong).

  3. RyanCavanaugh commented on Sep 26, 2024

    @RyanCavanaugh
    Member
  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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