Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
erikdstock opened this issue Sep 26, 2024 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@erikdstock
Copy link

erikdstock commented Sep 26, 2024

๐Ÿ”Ž 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;}
// ...
}
@jcalz
Copy link
Contributor

jcalz commented Sep 26, 2024

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

@erikdstock
Copy link
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).

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 26, 2024
@RyanCavanaugh
Copy link
Member

#26255 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants