-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The original implementation from `tyeppark` does not work with union type.
- Loading branch information
Showing
2 changed files
with
34 additions
and
16 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 |
---|---|---|
@@ -1,14 +1,23 @@ | ||
/** | ||
* Checks if two types are equal. | ||
* Borrow from `typepark`. | ||
* The simple `A extends B ? B extends A ? true : false : false` | ||
* does not work with boolean type. | ||
*/ | ||
export type Equal<A, B, Then = true, Else = false> = | ||
(<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) | ||
? Then : Else | ||
export type IsEqual<A, B> = Equal<A, B> | ||
|
||
export type NotEqual<A, B, Then = true, Else = false> = | ||
(<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? Else : Then | ||
export type IsNotEqual<A, B> = NotEqual<A, B> | ||
/** | ||
* `<T>() => T extends A ? 1 : 2` idea originate from `typepark` | ||
* But it does not work with union types. | ||
* | ||
* `<T>() => T extends A` is a trick to create an inferred type `T`. | ||
* This is needed for `boolean`, `string`, and `number`, | ||
* as they supports literal types. | ||
*/ | ||
|
||
/** | ||
* Checks if two types are equal. | ||
*/ | ||
export type Equal<A, B, Then = true, Else = false> = | ||
[A, B] extends [object, object] | ||
? (A extends B ? B extends A ? Then : Else : Else) | ||
: (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? Then : Else | ||
export type IsEqual<A, B> = Equal<A, B> | ||
|
||
export type NotEqual<A, B, Then = true, Else = false> = | ||
[A, B] extends [object, object] | ||
? (A extends B ? B extends A ? Else : Then : Then) | ||
: (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? Else : Then | ||
export type IsNotEqual<A, B> = NotEqual<A, B> |