Skip to content

Commit

Permalink
feat(types): IsUndefined, IfUndefined
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 59d4e0e commit 500173d
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-undefined.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Type Tests - IfUndefined
* @module tutils/types/tests/unit-d/IfUndefined
*/

import type TestSubject from '../if-undefined'

describe('unit-d:types/IfUndefined', () => {
type False = false
type True = true

it('should equal False if IsUndefined<T> extends false', () => {
expectTypeOf<TestSubject<undefined[], True, False>>().toEqualTypeOf<False>()
})

it('should equal True if IsUndefined<T> extends true', () => {
expectTypeOf<TestSubject<undefined, True, False>>().toEqualTypeOf<True>()
})
})
16 changes: 16 additions & 0 deletions src/types/__tests__/is-undefined.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Type Tests - IsUndefined
* @module tutils/types/tests/unit-d/IsUndefined
*/

import type TestSubject from '../is-undefined'

describe('unit-d:types/IsUndefined', () => {
it('should equal false if T does not extend undefined', () => {
expectTypeOf<TestSubject<null>>().toEqualTypeOf<false>()
})

it('should equal true if T extends undefined', () => {
expectTypeOf<TestSubject<undefined>>().toEqualTypeOf<true>()
})
})
20 changes: 20 additions & 0 deletions src/types/if-undefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file Type Definitions - IfUndefined
* @module tutils/types/IfUndefined
*/

import type IsUndefined from './is-undefined'

/**
* Conditional type that resolves depending on whether or not `T` extends
* `undefined`.
*
* @see {@linkcode IsUndefined}
*
* @template T - Type to evaluate
* @template True - Type if `T` extends `undefined`
* @template False - Type if `T` does not extend `undefined`
*/
type IfUndefined<T, True, False> = IsUndefined<T> extends true ? True : False

export type { IfUndefined as default }
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type { default as IfPrimitive } from './if-primitive'
export type { default as IfString } from './if-string'
export type { default as IfSymbol } from './if-symbol'
export type { default as IfTuple } from './if-tuple'
export type { default as IfUndefined } from './if-undefined'
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 @@ -53,6 +54,7 @@ export type { default as IsPrimitive } from './is-primitive'
export type { default as IsString } from './is-string'
export type { default as IsSymbol } from './is-symbol'
export type { default as IsTuple } from './is-tuple'
export type { default as IsUndefined } from './is-undefined'
export type { default as Join } from './join'
export type { default as JsonArray } from './json-array'
export type { default as JsonObject } from './json-object'
Expand Down
13 changes: 13 additions & 0 deletions src/types/is-undefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Type Definitions - IsUndefined
* @module tutils/types/IsUndefined
*/

/**
* Returns a boolean indicating if `T` extends `undefined`.
*
* @template T - Type to evaluate
*/
type IsUndefined<T> = T extends undefined ? true : false

export type { IsUndefined as default }

0 comments on commit 500173d

Please sign in to comment.