Skip to content

Commit

Permalink
feat(types): IsNever, IfNever
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 df660e8 commit 1cc2097
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-never.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Type Tests - IfNever
* @module tutils/types/tests/unit-d/IfNever
*/

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

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

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

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

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

describe('unit-d:types/IsNever', () => {
it('should equal false if [T] does not extend [never]', () => {
expectTypeOf<TestSubject<bigint>>().toEqualTypeOf<false>()
})

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

import type IsNever from './is-never'

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

export type { IfNever as default }
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ export type { default as IfBigInt } from './if-big-int'
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 IndexSignature } from './index-signature'
export type { default as IsAny } from './is-any'
export type { default as IsArray } from './is-array'
export type { default as IsBigInt } from './is-big-int'
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 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-never.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Type Definitions - IsNever
* @module tutils/types/IsNever
*/

/**
* Returns a boolean indicating if `T` is type `never`.
*
* @template T - Type to evaluate
*/
type IsNever<T> = [T] extends [never] ? true : false

export type { IsNever as default }

0 comments on commit 1cc2097

Please sign in to comment.