You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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;}
// ...
}
The text was updated successfully, but these errors were encountered:
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
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).
๐ Search Terms
array.includes, types
๐ Version & Regression Information
โฏ 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 typestring[]
. 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:
The text was updated successfully, but these errors were encountered: