Skip to content

Commit

Permalink
Narrow the searched element type in setHas and arrayIncludes (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
tychenjiajun authored Mar 16, 2022
1 parent 18b77d6 commit 04f2818
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions source/array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ if (arrayIncludes(values, valueToCheck)) {
@category Improved builtin
@category Type guard
*/
export function arrayIncludes<Type>(
export function arrayIncludes<Type extends SuperType, SuperType = unknown>(
array: Type[] | readonly Type[],
item: unknown,
item: SuperType,
fromIndex?: number,
): item is Type {
return array.includes(item as Type, fromIndex);
Expand Down
4 changes: 2 additions & 2 deletions source/set-has.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ if (setHas(valueSet, valueToCheck)) {
@category Improved builtin
@category Type guard
*/
export function setHas<Type>(
export function setHas<Type extends SuperType, SuperType = unknown>(
set: ReadonlySet<Type>,
item: unknown,
item: SuperType,
): item is Type {
return set.has(item as Type);
}
8 changes: 8 additions & 0 deletions test/array-includes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import test from 'ava';
import {expectTypeOf} from 'expect-type';
import {arrayIncludes} from '../source/index.js';

test('arrayIncludes()', t => {
const values = ['a', 'b', 'c'] as const;
const validValue: unknown = 'a';
const invalidValue: unknown = 'd';
const invalidTypedValue = 1;
let testValueType: typeof values[number];

expectTypeOf(values).items.toMatchTypeOf<typeof validValue>();
expectTypeOf(values).items.toMatchTypeOf<typeof invalidValue>();
expectTypeOf(values).items.not.toMatchTypeOf<typeof invalidTypedValue>();

t.true(arrayIncludes(values, validValue));
t.false(arrayIncludes(values, invalidValue));
// @ts-expect-error
t.false(arrayIncludes(values, invalidTypedValue));

if (arrayIncludes(values, validValue)) {
// @ts-expect-error
Expand Down
8 changes: 8 additions & 0 deletions test/set-has.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import test from 'ava';
import {expectTypeOf} from 'expect-type';
import {setHas} from '../source/index.js';

test('setHas()', t => {
const values = ['a', 'b', 'c'] as const;
const valueSet = new Set(values);
const validValue: unknown = 'a';
const invalidValue: unknown = 'd';
const invalidTypedValue = 1;
let testValueType: typeof values[number];

expectTypeOf(values).items.toMatchTypeOf<typeof validValue>();
expectTypeOf(values).items.toMatchTypeOf<typeof invalidValue>();
expectTypeOf(values).items.not.toMatchTypeOf<typeof invalidTypedValue>();

t.true(setHas(valueSet, validValue));
t.false(setHas(valueSet, invalidValue));
// @ts-expect-error
t.false(setHas(valueSet, invalidTypedValue));

// eslint-disable-next-line unicorn/prefer-ternary
if (setHas(valueSet, validValue)) {
Expand Down

0 comments on commit 04f2818

Please sign in to comment.