From 118ddc61f093ede92adcb82ca7212ee0f3a02389 Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Mon, 15 May 2023 17:43:39 -0400 Subject: [PATCH] feat(types): `IsEqual` Signed-off-by: Lexus Drumgold --- src/types/__tests__/is-equal.spec-d.ts | 18 ++++++++++++++++++ src/types/index.ts | 1 + src/types/is-equal.ts | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/types/__tests__/is-equal.spec-d.ts create mode 100644 src/types/is-equal.ts diff --git a/src/types/__tests__/is-equal.spec-d.ts b/src/types/__tests__/is-equal.spec-d.ts new file mode 100644 index 00000000..df773129 --- /dev/null +++ b/src/types/__tests__/is-equal.spec-d.ts @@ -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>().toEqualTypeOf() + expectTypeOf>().toEqualTypeOf() + }) + + it('should equal true if A and B are equal', () => { + expectTypeOf>().toEqualTypeOf() + expectTypeOf>().toEqualTypeOf() + }) +}) diff --git a/src/types/index.ts b/src/types/index.ts index 07382c39..fd29d33e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -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' diff --git a/src/types/is-equal.ts b/src/types/is-equal.ts new file mode 100644 index 00000000..a28ce86e --- /dev/null +++ b/src/types/is-equal.ts @@ -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 = (() => T extends A ? 1 : 2) extends () => T extends B + ? 1 + : 2 + ? true + : false + +export type { IsEqual as default }