Skip to content

Commit

Permalink
Update lib.es2016.array.include.d.ts
Browse files Browse the repository at this point in the history
Array's `include` function checks whether or not the `searchElement` exists in it, and returns the appropriate boolean value as an answer.
However, in the current implementation, TypeScript is requesting the type of `searchElement` to be a member of the array, instead of receiving any value as `searchElement`.
This in turn forces the user to know in advance whether or not the `searchElement` is a member of the array, which defeats the whole purpose of using the 'include' function. The way users currently workaround that problem is by overriding the TypeScript mechanism by using "as", as suggested by @sandersn in here: microsoft#33200 (comment) . See also https://stackoverflow.com/questions/43804805/check-if-value-exists-in-enum-in-typescript .

I believe that the purpose of the `include` function is to allow any variable, even if its type is unknown, to be searched in the array and get a boolean answer, and therefore I suggest the following changes.

Playground:
https://www.typescriptlang.org/play?#code/LAKApgdgrgtgBAMQEoFUCSAVAynA3qOOAQQAUSAZAUTgF44ByAQ3oBoC4AhIgOR6NoYAjVqAC+oUADMoEAMYAXAJYB7CHEUBnAGqMANooAmWKAAcTygE7ywBhBaiL5ACigawFtBBNR5ALjgyANYQygDuEACU-oLKyrpgjGr4IIQA9ABUEilwGAAWYHCScbphihAA5nD6EAWacPL5cBpQ5eVgGtYGcKGMAJ71yt2WgYwWyjJdjixwiZPyQxaBGtOCPt25-QaDaHC5jABuBfKDyocWFoYFGL0mYFiyFybyAPzsAOobcFtwO3uHAzMNG4rPU9vMGgVXO5PN55nVkOhsNNQrlFPEfvR4CF5s0LLV5ni9Lp+nUnLNQYx5PQNKCCii4gVvBZzG44MpJAENGVKgADMqyXRQAxgHkRV4gdiEPHyKAWNQAeUEACswAoAHT7PRQdpOBGYLARNX8wXCjQuYEwtaMGl67ARADckrg7HSqXY0tlCuVqvkGq1OttBqNchNOqhHi8PgdcFSqRyuTGoRpiTg7jGFn8RAs5VgkHm7PqNwK9CCIXC9HUNOxgK55QgjEE6OOcBMo0YMDA1gsbI58iLDED9DVOX7JYgwTCEArdWr1tr9cbR0GfduA9Q+rVXF4vCHTgATABmAAsAFYImIsrJVBoGWqSuUnJodPojKZzFYbHYHM5A2rSBRKAiaNYzgSgAA9bgUGx-HkewwFAK8IBveI72UB8nz0QxjDMSxOi-RwnHoYQgPtGM43AyDOhguCEOvW970fbRMNfHCP1sewCPoWR6BIsjQIgn1oMKPQ3FopD6LQxjnywt9cM-DjnGgXRdF4kCKMEgx-EkET4JARDkLAVD0KYl9sPfPCFKcXBRFU8iBKgzThN0NwgA
  • Loading branch information
Asaf-S authored Sep 7, 2021
1 parent 3c8e45b commit c91d350
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/lib.es2016.array.include.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface Array<T> {
* @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(searchElement: unknown, fromIndex?: number): boolean;
}

interface ReadonlyArray<T> {
Expand All @@ -33,7 +33,7 @@ interface ReadonlyArray<T> {
* @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(searchElement: unknown, fromIndex?: number): boolean;
}

interface Int8Array {
Expand All @@ -42,7 +42,7 @@ interface Int8Array {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
includes(searchElement: unknown, fromIndex?: number): boolean;
}

interface Uint8Array {
Expand All @@ -51,7 +51,7 @@ interface Uint8Array {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
includes(searchElement: unknown, fromIndex?: number): boolean;
}

interface Uint8ClampedArray {
Expand All @@ -60,7 +60,7 @@ interface Uint8ClampedArray {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
includes(searchElement: unknown, fromIndex?: number): boolean;
}

interface Int16Array {
Expand All @@ -69,7 +69,7 @@ interface Int16Array {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
includes(searchElement: unknown, fromIndex?: number): boolean;
}

interface Uint16Array {
Expand All @@ -78,7 +78,7 @@ interface Uint16Array {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
includes(searchElement: unknown, fromIndex?: number): boolean;
}

interface Int32Array {
Expand All @@ -87,7 +87,7 @@ interface Int32Array {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
includes(searchElement: unknown, fromIndex?: number): boolean;
}

interface Uint32Array {
Expand All @@ -96,7 +96,7 @@ interface Uint32Array {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
includes(searchElement: unknown, fromIndex?: number): boolean;
}

interface Float32Array {
Expand All @@ -105,7 +105,7 @@ interface Float32Array {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
includes(searchElement: unknown, fromIndex?: number): boolean;
}

interface Float64Array {
Expand All @@ -114,5 +114,5 @@ interface Float64Array {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: number, fromIndex?: number): boolean;
}
includes(searchElement: unknown, fromIndex?: number): boolean;
}

0 comments on commit c91d350

Please sign in to comment.