-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
066ec01
commit f988c40
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
An alternative to `Array.prototype.includes` that properly acts as a type guard. | ||
It was [rejected](https://github.com/microsoft/TypeScript/issues/26255#issuecomment-748211891) from being done in TypeScript itself. | ||
@example | ||
``` | ||
import {arrayIncludes} from 'ts-extras'; | ||
const values = ['a', 'b', 'c'] as const; | ||
const valueToCheck: unknown = 'a'; | ||
if (arrayIncludes(values, valueToCheck)) { | ||
// We now know that the value is of type `typeof values[number]`. | ||
} | ||
``` | ||
*/ | ||
export function arrayIncludes<Type>( | ||
array: Type[] | readonly Type[], | ||
item: unknown, | ||
fromIndex?: number, | ||
): item is Type { | ||
return array.includes(item as Type, fromIndex); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import test from 'ava'; | ||
import {arrayIncludes} from '../source/index.js'; | ||
|
||
test('arrayIncludes()', t => { | ||
const values = ['a', 'b', 'c'] as const; | ||
const validValue: unknown = 'a'; | ||
const invalidValue: unknown = 'd'; | ||
let testValueType: typeof values[number]; | ||
|
||
t.true(arrayIncludes(values, validValue)); | ||
t.false(arrayIncludes(values, invalidValue)); | ||
|
||
if (arrayIncludes(values, validValue)) { | ||
// @ts-expect-error | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
values.push(); // Ensure readonly array is still readonly. | ||
|
||
testValueType = validValue; | ||
} else { | ||
// @ts-expect-error | ||
testValueType = validValue; | ||
|
||
// @ts-expect-error | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
values.push(); // Ensure readonly array is still readonly. | ||
} | ||
|
||
t.is(testValueType, 'a'); | ||
}); |