Skip to content

Commit

Permalink
feat(types): IsAny, IfAny
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 c07d58e commit b63526d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/types/__tests__/if-any.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Type Tests - IfAny
* @module tutils/types/tests/unit-d/IfAny
*/

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

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

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

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

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

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

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

import type IsAny from './is-any'

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

export type { IfAny as default }
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export type { default as Fallback } from './fallback'
export type { default as FIXME } from './fixme'
export type { default as Fn } from './fn'
export type { default as Get } from './get'
export type { default as IfAny } from './if-any'
export type { default as IndexSignature } from './index-signature'
export type { default as IsAny } from './is-any'
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-any.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Type Definitions - IsAny
* @module tutils/types/IsAny
*/

/**
* Returns a boolean indicating if `T` is type `any`.
*
* @template T - Type to evaluate
*/
type IsAny<T> = 0 extends T & 1 ? true : false

export type { IsAny as default }

0 comments on commit b63526d

Please sign in to comment.