Skip to content

Commit

Permalink
feat(types): IsEqual
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 15, 2023
1 parent 35fdbd3 commit 118ddc6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/types/__tests__/is-equal.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file Type Tests - IsEqual
* @module tutils/types/tests/unit-d/IsEqual
*/

import type TestSubject from '../is-equal'

describe('unit-d:types/IsEqual', () => {
it('should equal false A and B are not equal', () => {
expectTypeOf<TestSubject<0, 1>>().toEqualTypeOf<false>()
expectTypeOf<TestSubject<20, { limit: 20 }>>().toEqualTypeOf<false>()
})

it('should equal true if A and B are equal', () => {
expectTypeOf<TestSubject<13, 13>>().toEqualTypeOf<true>()
expectTypeOf<TestSubject<[1, 2], [1, 2]>>().toEqualTypeOf<true>()
})
})
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type { default as IsAny } from './is-any'
export type { default as IsArray } from './is-array'
export type { default as IsBigInt } from './is-big-int'
export type { default as IsBoolean } from './is-boolean'
export type { default as IsEqual } from './is-equal'
export type { default as IsFunction } from './is-function'
export type { default as IsJsonPrimitive } from './is-json-primitive'
export type { default as IsNever } from './is-never'
Expand Down
21 changes: 21 additions & 0 deletions src/types/is-equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @file Type Definitions - IsEqual
* @module tutils/types/IsEqual
*/

/**
* Returns a boolean indicating if `A` and `B` are equal.
*
* @see https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
* @see https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
*
* @template A - First type to evaluate
* @template B - Second type to evaluate
*/
type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B
? 1
: 2
? true
: false

export type { IsEqual as default }

0 comments on commit 118ddc6

Please sign in to comment.