Skip to content

Commit

Permalink
[flow] type refinement for Array.includes
Browse files Browse the repository at this point in the history
Summary: It is a common pattern to use `[a, b, c].includes(foo)` instead of `foo === a || foo === b || foo === c`, but today only the second pattern be used for type refinement.

Reviewed By: panagosg7

Differential Revision: D69577322

fbshipit-source-id: 88192132af64e63a6c5ee07104d61b9cf6d3bbe6
  • Loading branch information
Naman Goel authored and facebook-github-bot committed Feb 21, 2025
1 parent 256ff21 commit 3be862c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ declare class $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: mixed, fromIndex?: number): boolean;
includes(searchElement: mixed, fromIndex?: number): implies searchElement is T;
/**
* Returns the index of the first occurrence of a value in an array.
* @param searchElement The value to locate in the array.
Expand Down
5 changes: 5 additions & 0 deletions tests/arrays/includes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare var x: number;

if (([1, 2, 3] as $ReadOnlyArray<1 | 2 | 3>).includes(x)) {
(x as 1 | 2 | 3);
}
4 changes: 2 additions & 2 deletions tests/autocomplete_from_h_to_l/autocomplete_from_h_to_l.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2391,7 +2391,7 @@ Flags: --pretty
},
{
"name":"?.includes",
"type":"void | ((searchElement: mixed, fromIndex?: number) => boolean)"
"type":"void | ((searchElement: mixed, fromIndex?: number) => implies searchElement is T)"
},
{
"name":"?.indexOf",
Expand Down Expand Up @@ -2500,7 +2500,7 @@ Flags: --pretty
},
{
"name":"includes",
"type":"(searchElement: mixed, fromIndex?: number) => boolean"
"type":"(searchElement: mixed, fromIndex?: number) => implies searchElement is T"
},
{
"name":"indexOf",
Expand Down

0 comments on commit 3be862c

Please sign in to comment.