Skip to content

Commit

Permalink
feat(types): IsUnknown, IfUnknown
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed May 13, 2023
1 parent 500173d commit 2079db9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/types/__tests__/if-unknown.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Type Tests - IfUnknown
* @module tutils/types/tests/unit-d/IfUnknown
*/

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

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

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

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

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

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

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

import type IsUnknown from './is-unknown'

/**
* Conditional type that resolves depending on whether or not `T` is type
* `unknown`.
*
* @see {@linkcode IsUnknown}
*
* @template T - Type to evaluate
* @template True - Type if `T` is type `unknown`
* @template False - Type if `T` is not type `unknown`
*/
type IfUnknown<T, True, False> = IsUnknown<T> extends true ? True : False

export type { IfUnknown as default }
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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 IfUnknown } from './if-unknown'
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 @@ -55,6 +56,7 @@ 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 IsUnknown } from './is-unknown'
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
19 changes: 19 additions & 0 deletions src/types/is-unknown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Type Definitions - IsUnknown
* @module tutils/types/IsUnknown
*/

import type IsNull from './is-null'

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

export type { IsUnknown as default }

0 comments on commit 2079db9

Please sign in to comment.