Skip to content

Commit

Permalink
Includes: Fix false-positive when the array contains null and you…
Browse files Browse the repository at this point in the history
… are checking against `undefined` (#413)
  • Loading branch information
JonghwanWon committed Jul 2, 2022
1 parent 96fe795 commit de5e7fc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 5 additions & 5 deletions source/includes.d.ts
Expand Up @@ -15,8 +15,8 @@ type hasRed<array extends any[]> = Includes<array, 'red'>;
@category Array
*/
export type Includes<Value extends readonly any[], Item> =
IsEqual<Value[0], Item> extends true
? true
: Value extends [Value[0], ...infer rest]
? Includes<rest, Item>
: false;
Value extends readonly [Value[0], ...infer rest]
? IsEqual<Value[0], Item> extends true
? true
: Includes<rest, Item>
: false;
6 changes: 6 additions & 0 deletions test-d/includes.ts
Expand Up @@ -32,6 +32,12 @@ expectType<false>(objectIncludes);
const objectIncludesPass: Includes<[{a: 1}], {a: 1}> = true;
expectType<true>(objectIncludesPass);

const nullIncludesUndefined: Includes<[null], undefined> = false;
expectType<false>(nullIncludesUndefined);

const nullIncludesNullPass: Includes<[null], null> = true;
expectType<true>(nullIncludesNullPass);

declare const anything: any;

expectError<Includes>(anything);
Expand Down

0 comments on commit de5e7fc

Please sign in to comment.