Skip to content

Commit

Permalink
feat(types): IsObject, IfObject
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 30, 2023
1 parent 9ae48c3 commit 1863acc
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/types/__tests__/if-object.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Type Tests - IfObject
* @module tutils/types/tests/unit-d/IfObject
*/

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

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

it('should equal False if IsObject<T> extends false', () => {
expectTypeOf<TestSubject<string, True, False>>().toEqualTypeOf<False>()
})

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

import type Fn from '../fn'
import type TestSubject from '../is-object'
import type ObjectCurly from '../object-curly'
import type Primitive from '../primitive'

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

it('should equal false if T is any', () => {
expectTypeOf<TestSubject<any>>().toEqualTypeOf<false>()
})

it('should equal false if T is never', () => {
expectTypeOf<TestSubject<never>>().toEqualTypeOf<false>()
})

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

import type IsObject from './is-object'

/**
* Returns a type that indicates if `T` is an `object`.
*
* @see {@linkcode IsObject}
*
* @template T - Type to evaluate
* @template True - Type if `T` is an `object`
* @template False - Type if `T` is not an `object`
*/
type IfObject<T, True, False> = IsObject<T> extends true ? True : False

export type { IfObject as default }
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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 IfObject } from './if-object'
export type { default as IfPrimitive } from './if-primitive'
export type { default as IfString } from './if-string'
export type { default as IfSymbol } from './if-symbol'
Expand All @@ -68,6 +69,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 IsObject } from './is-object'
export type { default as IsPrimitive } from './is-primitive'
export type { default as IsString } from './is-string'
export type { default as IsSymbol } from './is-symbol'
Expand Down
15 changes: 15 additions & 0 deletions src/types/is-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @file Type Definitions - IsObject
* @module tutils/types/IsObject
*/

import type IfAnyOrNever from './if-any-or-never'

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

export type { IsObject as default }

0 comments on commit 1863acc

Please sign in to comment.