forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: issue sindresorhus#788 added ifEqual guard
- Loading branch information
1 parent
eb96609
commit 5995215
Showing
3 changed files
with
43 additions
and
0 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,24 @@ | ||
import type { IsEqual } from './is-equal'; | ||
|
||
/** | ||
* An if-else-like type that resolves depending on whether the two given types are equal. | ||
* | ||
* @see {@link IsEqual} | ||
* | ||
* @example | ||
* ``` | ||
* import type { IfEqual } from 'type-fest'; | ||
* | ||
* type Result1 = IfEqual<string, string, 'Equal', 'NotEqual'>; | ||
* //=> 'Equal' | ||
* | ||
* type Result2 = IfEqual<string, number, 'Equal', 'NotEqual'>; | ||
* //=> 'NotEqual' | ||
* ``` | ||
* | ||
* @category Type Guard | ||
* @category Utilities | ||
*/ | ||
export type IfEqual<A, B, TypeIfEqual = true, TypeIfNotEqual = false> = ( | ||
IsEqual<A, B> extends true ? TypeIfEqual : TypeIfNotEqual | ||
); |
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,18 @@ | ||
import { expectType, expectError } from 'tsd'; | ||
import type { IfEqual } from '../index'; | ||
|
||
// `IfEqual` should return `true`/`false` if only `A` and `B` are specified | ||
expectType<IfEqual<string, string>>(true); | ||
expectType<IfEqual<string, number>>(false); | ||
|
||
// `IfEqual` with custom types for equal or not equal cases | ||
expectType<IfEqual<string, string, 'Equal', 'NotEqual'>>('Equal'); | ||
expectType<IfEqual<string, number, 'Equal', 'NotEqual'>>('NotEqual'); | ||
|
||
// `IfEqual` should correctly handle complex types | ||
expectType<IfEqual<{ a: number }, { a: number }>>(true); | ||
expectType<IfEqual<{ a: number }, { b: number }>>(false); | ||
|
||
// Missing generic parameters | ||
expectError<IfEqual<string>>(undefined); | ||
expectError<IfEqual>(undefined); |