Skip to content

Commit

Permalink
feat(types): IsPrimitive, IfPrimitive
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 1dc5f13 commit acffb52
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/types/__tests__/if-primitive.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file Type Tests - IfPrimitive
* @module tutils/types/tests/unit-d/IfPrimitive
*/

import type TestSubject from '../if-primitive'
import type Primitive from '../primitive'

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

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

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

import type TestSubject from '../is-primitive'
import type Primitive from '../primitive'

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

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

import type IsPrimitive from './is-primitive'

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

export type { IfPrimitive as default }
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type { default as IfNil } from './if-nil'
export type { default as IfNull } from './if-null'
export type { default as IfNumber } from './if-number'
export type { default as IfNumeric } from './if-numeric'
export type { default as IfPrimitive } from './if-primitive'
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 @@ -45,6 +46,7 @@ export type { default as IsNil } from './is-nil'
export type { default as IsNull } from './is-null'
export type { default as IsNumber } from './is-number'
export type { default as IsNumeric } from './is-numeric'
export type { default as IsPrimitive } from './is-primitive'
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-primitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @file Type Definitions - IsPrimitive
* @module tutils/types/IsPrimitive
*/

import type Primitive from './primitive'

/**
* Returns a boolean indicating if `T` extends {@linkcode Primitive}.
*
* @template T - Type to evaluate
*/
type IsPrimitive<T> = T extends Primitive ? true : false

export type { IsPrimitive as default }

0 comments on commit acffb52

Please sign in to comment.