-
-
Notifications
You must be signed in to change notification settings - Fork 569
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 <sindresorhus@gmail.com>
- Loading branch information
1 parent
55426a0
commit bf4fc28
Showing
5 changed files
with
73 additions
and
9 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
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,39 @@ | ||
import {expectError, expectType} from 'tsd'; | ||
import {Includes} from '..'; | ||
|
||
const includesEmptyArray: Includes<[], 'abc'> = false; | ||
expectType<false>(includesEmptyArray); | ||
|
||
const includesSingleItemArray: Includes<['colors'], 'colors'> = true; | ||
expectType<true>(includesSingleItemArray); | ||
|
||
const includesComplexMultiTypeArray: Includes<[ | ||
{ | ||
prop: 'value'; | ||
num: 5; | ||
anotherArr: [1, '5', false]; | ||
}, | ||
true, | ||
null, | ||
'abcd' | ||
], 'abc'> = false; | ||
expectType<false>(includesComplexMultiTypeArray); | ||
|
||
const noExtendsProblem: Includes<[boolean], true> = false; | ||
expectType<false>(noExtendsProblem); | ||
|
||
const objectIncludes: Includes<[{}], {a: 1}> = false; | ||
expectType<false>(objectIncludes); | ||
|
||
const objectIncludesPass: Includes<[{a: 1}], {a: 1}> = true; | ||
expectType<true>(objectIncludesPass); | ||
|
||
declare const anything: any; | ||
|
||
expectError<Includes>(anything); | ||
|
||
expectError<Includes<['my', 'array', 'has', 'stuff']>>(anything); | ||
|
||
expectError<Includes<'why a string?', 5>>(anything); | ||
|
||
expectError<Includes<{key: 'value'}, 7>>(anything); |
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,31 @@ | ||
/** | ||
Returns a boolean for whether given two types are equal. | ||
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650 | ||
*/ | ||
type IsEqual<T, U> = | ||
(<G>() => G extends T ? 1 : 2) extends | ||
(<G>() => G extends U ? 1 : 2) | ||
? true | ||
: false; | ||
|
||
/** | ||
Returns a boolean for whether the given array includes the given item. | ||
This can be useful if another type wants to make a decision based on whether the array includes that item. | ||
@example | ||
``` | ||
import {Includes} from 'type-fest'; | ||
type hasRed<array extends any[]> = Includes<array, 'red'>; | ||
``` | ||
@category Utilities | ||
*/ | ||
export type Includes<Value extends any[], Item> = | ||
IsEqual<Value[0], Item> extends true | ||
? true | ||
: Value extends [Value[0], ...infer rest] | ||
? Includes<rest, Item> | ||
: false; |
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