From 7ea934786897c765a16fd4679f7a7359fd5f76f7 Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Sat, 13 May 2023 05:03:14 -0400 Subject: [PATCH] feat(types): `IsNull`, `IfNull` Signed-off-by: Lexus Drumgold --- src/types/__tests__/if-null.spec-d.ts | 19 +++++++++++++++++++ src/types/__tests__/is-null.spec-d.ts | 16 ++++++++++++++++ src/types/if-null.ts | 20 ++++++++++++++++++++ src/types/index.ts | 2 ++ src/types/is-null.ts | 13 +++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 src/types/__tests__/if-null.spec-d.ts create mode 100644 src/types/__tests__/is-null.spec-d.ts create mode 100644 src/types/if-null.ts create mode 100644 src/types/is-null.ts diff --git a/src/types/__tests__/if-null.spec-d.ts b/src/types/__tests__/if-null.spec-d.ts new file mode 100644 index 00000000..cc3393ec --- /dev/null +++ b/src/types/__tests__/if-null.spec-d.ts @@ -0,0 +1,19 @@ +/** + * @file Type Tests - IfNull + * @module tutils/types/tests/unit-d/IfNull + */ + +import type TestSubject from '../if-null' + +describe('unit-d:types/IfNull', () => { + type False = false + type True = true + + it('should equal False if IsNull extends false', () => { + expectTypeOf>().toEqualTypeOf() + }) + + it('should equal True if IsNull extends true', () => { + expectTypeOf>().toEqualTypeOf() + }) +}) diff --git a/src/types/__tests__/is-null.spec-d.ts b/src/types/__tests__/is-null.spec-d.ts new file mode 100644 index 00000000..76927237 --- /dev/null +++ b/src/types/__tests__/is-null.spec-d.ts @@ -0,0 +1,16 @@ +/** + * @file Type Tests - IsNull + * @module tutils/types/tests/unit-d/IsNull + */ + +import type TestSubject from '../is-null' + +describe('unit-d:types/IsNull', () => { + it('should equal false if T does not extend null', () => { + expectTypeOf>().toEqualTypeOf() + }) + + it('should equal true if T extends null', () => { + expectTypeOf>().toEqualTypeOf() + }) +}) diff --git a/src/types/if-null.ts b/src/types/if-null.ts new file mode 100644 index 00000000..990e0b82 --- /dev/null +++ b/src/types/if-null.ts @@ -0,0 +1,20 @@ +/** + * @file Type Definitions - IfNull + * @module tutils/types/IfNull + */ + +import type IsNull from './is-null' + +/** + * Conditional type that resolves depending on whether or not `T` extends + * `null`. + * + * @see {@linkcode IsNull} + * + * @template T - Type to evaluate + * @template True - Type if `T` extends `null` + * @template False - Type if `T` does not extend `null` + */ +type IfNull = IsNull extends true ? True : False + +export type { IfNull as default } diff --git a/src/types/index.ts b/src/types/index.ts index 406db3ee..03aba645 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -30,6 +30,7 @@ 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 IfNull } from './if-null' export type { default as IndexSignature } from './index-signature' export type { default as IsAny } from './is-any' export type { default as IsArray } from './is-array' @@ -39,6 +40,7 @@ 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 IsNull } from './is-null' 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-null.ts b/src/types/is-null.ts new file mode 100644 index 00000000..5c96505d --- /dev/null +++ b/src/types/is-null.ts @@ -0,0 +1,13 @@ +/** + * @file Type Definitions - IsNull + * @module tutils/types/IsNull + */ + +/** + * Returns a boolean indicating if `T` extends `null`. + * + * @template T - Type to evaluate + */ +type IsNull = T extends null ? true : false + +export type { IsNull as default }