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
{{ message }}
Repository navigation
Array.includes() should accept any arg that extends the array's type #60073
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;}
// ...
}
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).
Activity
jcalz commentedon 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 #14520erikdstock commentedon Sep 26, 2024
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 commentedon Sep 26, 2024
#26255 (comment)