From d132941acba63ec82ee197455b22c631b2a83c4b Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Sat, 13 May 2023 05:00:02 -0400 Subject: [PATCH] feat(types): `IsNil`, `IfNil` Signed-off-by: Lexus Drumgold --- src/types/__tests__/if-nil.spec-d.ts | 20 ++++++++++++++++++++ src/types/__tests__/is-nil.spec-d.ts | 17 +++++++++++++++++ src/types/if-nil.ts | 19 +++++++++++++++++++ src/types/index.ts | 2 ++ src/types/is-nil.ts | 15 +++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 src/types/__tests__/if-nil.spec-d.ts create mode 100644 src/types/__tests__/is-nil.spec-d.ts create mode 100644 src/types/if-nil.ts create mode 100644 src/types/is-nil.ts diff --git a/src/types/__tests__/if-nil.spec-d.ts b/src/types/__tests__/if-nil.spec-d.ts new file mode 100644 index 00000000..139225ac --- /dev/null +++ b/src/types/__tests__/if-nil.spec-d.ts @@ -0,0 +1,20 @@ +/** + * @file Type Tests - IfNil + * @module tutils/types/tests/unit-d/IfNil + */ + +import type TestSubject from '../if-nil' +import type NIL from '../nil' + +describe('unit-d:types/IfNil', () => { + type False = false + type True = true + + it('should equal False if IsNil extends false', () => { + expectTypeOf>().toEqualTypeOf() + }) + + it('should equal True if IsNil extends true', () => { + expectTypeOf>().toEqualTypeOf() + }) +}) diff --git a/src/types/__tests__/is-nil.spec-d.ts b/src/types/__tests__/is-nil.spec-d.ts new file mode 100644 index 00000000..ccaaa1fe --- /dev/null +++ b/src/types/__tests__/is-nil.spec-d.ts @@ -0,0 +1,17 @@ +/** + * @file Type Tests - IsNil + * @module tutils/types/tests/unit-d/IsNil + */ + +import type TestSubject from '../is-nil' +import type NIL from '../nil' + +describe('unit-d:types/IsNil', () => { + it('should equal false if T does not extend NIL', () => { + expectTypeOf>().toEqualTypeOf() + }) + + it('should equal true if T extends NIL', () => { + expectTypeOf>().toEqualTypeOf() + }) +}) diff --git a/src/types/if-nil.ts b/src/types/if-nil.ts new file mode 100644 index 00000000..1b99ff3f --- /dev/null +++ b/src/types/if-nil.ts @@ -0,0 +1,19 @@ +/** + * @file Type Definitions - IfNil + * @module tutils/types/IfNil + */ + +import type IsNil from './is-nil' + +/** + * Conditional type that resolves depending on whether or not `T` extends `NIL`. + * + * @see {@linkcode IsNil} + * + * @template T - Type to evaluate + * @template True - Type if `T` is `nil` + * @template False - Type if `T` is not `nil` + */ +type IfNil = IsNil extends true ? True : False + +export type { IfNil as default } diff --git a/src/types/index.ts b/src/types/index.ts index a6cb681c..406db3ee 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -29,6 +29,7 @@ export type { default as IfBoolean } from './if-boolean' export type { default as IfFunction } from './if-function' export type { default as IfJsonPrimitive } from './if-json-primitive' export type { default as IfNever } from './if-never' +export type { default as IfNil } from './if-nil' export type { default as IndexSignature } from './index-signature' export type { default as IsAny } from './is-any' export type { default as IsArray } from './is-array' @@ -37,6 +38,7 @@ export type { default as IsBoolean } from './is-boolean' 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' +export type { default as IsNil } from './is-nil' export type { default as IsTuple } from './is-tuple' export type { default as Join } from './join' export type { default as JsonArray } from './json-array' diff --git a/src/types/is-nil.ts b/src/types/is-nil.ts new file mode 100644 index 00000000..29e40504 --- /dev/null +++ b/src/types/is-nil.ts @@ -0,0 +1,15 @@ +/** + * @file Type Definitions - IsNil + * @module tutils/types/IsNil + */ + +import type NIL from './nil' + +/** + * Returns a boolean indicating if `T` extends {@linkcode NIL}. + * + * @template T - Type to evaluate + */ +type IsNil = T extends NIL ? true : false + +export type { IsNil as default }