Skip to content

Commit

Permalink
feat(types): IsNil, IfNil
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 1cc2097 commit d132941
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/types/__tests__/if-nil.spec-d.ts
Original file line number Diff line number Diff line change
@@ -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<T> extends false', () => {
expectTypeOf<TestSubject<NIL[], True, False>>().toEqualTypeOf<False>()
})

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

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

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

export type { IsNil as default }

0 comments on commit d132941

Please sign in to comment.