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
6950398
commit e5a3a57
Showing
10 changed files
with
67 additions
and
17 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
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
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
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,30 @@ | ||
/** | ||
Returns a boolean for whether the two given types are equal. | ||
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650 | ||
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796 | ||
Use-cases: | ||
- If you want to make a conditional branch based on the result of a comparison of two types. | ||
@example | ||
``` | ||
import type {IsEqual} from 'type-fest'; | ||
// This type returns a boolean for whether the given array includes the given item. | ||
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal. | ||
type Includes<Value extends readonly any[], Item> = | ||
Value extends readonly [Value[0], ...infer rest] | ||
? IsEqual<Value[0], Item> extends true | ||
? true | ||
: Includes<rest, Item> | ||
: false; | ||
``` | ||
@category Utilities | ||
*/ | ||
export type IsEqual<A, B> = | ||
(<G>() => G extends A ? 1 : 2) extends | ||
(<G>() => G extends B ? 1 : 2) | ||
? true | ||
: 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
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,28 @@ | ||
import {expectError, expectType} from 'tsd'; | ||
import type {IsEqual} from '../index'; | ||
|
||
const notEqualNumberAndString: IsEqual<number, string> = false; | ||
expectType<false>(notEqualNumberAndString); | ||
|
||
const equalNumbers: IsEqual<1, 1> = true; | ||
expectType<true>(equalNumbers); | ||
|
||
const notEqualAnyAndNumber: IsEqual<any, number> = false; | ||
expectType<false>(notEqualAnyAndNumber); | ||
|
||
const notEqualUnionAndNumber: IsEqual<1 | 2, 1> = false; | ||
expectType<false>(notEqualUnionAndNumber); | ||
|
||
const notEqualAnyAndNever: IsEqual<any, never> = false; | ||
expectType<false>(notEqualAnyAndNever); | ||
|
||
const notEqualArrayOfAnyAndArrayOfNever: IsEqual<[any], [never]> = false; | ||
expectType<false>(notEqualArrayOfAnyAndArrayOfNever); | ||
|
||
declare const anything: any; | ||
|
||
// Missing all generic parameters. | ||
expectError<IsEqual>(anything); | ||
|
||
// Missing `Y` generic parameter. | ||
expectError<IsEqual<number>>(anything); |