Skip to content

Commit

Permalink
feat(types): IsFunction, IfFunction
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 0501d14 commit 298f43b
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const config = {
files: [
'src/types/built-in.ts',
'src/types/exact-optional-property-types.ts',
'src/types/fn.ts',
'src/types/overwrite.ts'
],
rules: {
Expand Down
20 changes: 20 additions & 0 deletions src/types/__tests__/if-function.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file Type Tests - IfFunction
* @module tutils/types/tests/unit-d/IfFunction
*/

import type Fn from '../fn'
import type TestSubject from '../if-function'

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

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

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

import type Fn from '../fn'
import type TestSubject from '../is-function'

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

it('should equal true if T extends Fn', () => {
expectTypeOf<TestSubject<Fn>>().toEqualTypeOf<true>()
})
})
2 changes: 1 addition & 1 deletion src/types/fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
* @template A - Arguments type
* @template R - Return type
*/
type Fn<A extends any[] = any[], R = any> = (...args: A) => R
type Fn<A extends any[] = any[], R = any> = Function & ((...args: A) => R)

export type { Fn as default }
20 changes: 20 additions & 0 deletions src/types/if-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file Type Definitions - IfFunction
* @module tutils/types/IfFunction
*/

import type IsFunction from './is-function'

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

export type { IfFunction as default }
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export type { default as IfAny } from './if-any'
export type { default as IfArray } from './if-array'
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 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 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-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @file Type Definitions - IsFunction
* @module tutils/types/IsFunction
*/

import type Fn from './fn'

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

export type { IsFunction as default }

0 comments on commit 298f43b

Please sign in to comment.