Skip to content

Commit

Permalink
feat(types): IsNull, IfNull
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 13, 2023
1 parent d132941 commit 7ea9347
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/types/__tests__/if-null.spec-d.ts
Original file line number Diff line number Diff line change
@@ -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<T> extends false', () => {
expectTypeOf<TestSubject<string[], True, False>>().toEqualTypeOf<False>()
})

it('should equal True if IsNull<T> extends true', () => {
expectTypeOf<TestSubject<null, True, False>>().toEqualTypeOf<True>()
})
})
16 changes: 16 additions & 0 deletions src/types/__tests__/is-null.spec-d.ts
Original file line number Diff line number Diff line change
@@ -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<TestSubject<boolean>>().toEqualTypeOf<false>()
})

it('should equal true if T extends null', () => {
expectTypeOf<TestSubject<null>>().toEqualTypeOf<true>()
})
})
20 changes: 20 additions & 0 deletions src/types/if-null.ts
Original file line number Diff line number Diff line change
@@ -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<T, True, False> = IsNull<T> extends true ? True : False

export type { IfNull as default }
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
13 changes: 13 additions & 0 deletions src/types/is-null.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends null ? true : false

export type { IsNull as default }

0 comments on commit 7ea9347

Please sign in to comment.