Skip to content

Commit

Permalink
feat(types): TypeGuard
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Aug 13, 2023
1 parent e00904b commit 36aa5f8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/types/__tests__/type-guard.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @file Type Tests - TypeGuard
* @module tutils/types/tests/unit-d/TypeGuard
*/

import type Vehicle from '#fixtures/types/vehicle'
import type TestSubject from '../type-guard'

describe('unit-d:types/TypeGuard', () => {
it('should guard T', () => {
// Arrange
type T = Vehicle

// Expect
expectTypeOf<TestSubject<T>>().guards.toEqualTypeOf<T>()
})
})
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export type { default as Trim } from './trim'
export type { default as TrimEnd } from './trim-end'
export type { default as TrimStart } from './trim-start'
export type { default as Tryit } from './tryit'
export type { default as TypeGuard } from './type-guard'
export type { default as TypedArray } from './typed-array'
export type { default as UnionToIntersection } from './union-to-intersection'
export type { default as UnionToTuple } from './union-to-tuple'
Expand Down
18 changes: 18 additions & 0 deletions src/types/type-guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file Type Definitions - TypeGuard
* @module tutils/types/TypeGuard
*/

/**
* Checks if `value` is of type `T`.
*
* @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
*
* @template T - Guarded value type
*
* @param {unknown} value - Value to check
* @return {value is T} `true` if `value` is of type `T`, `false` otherwise
*/
type TypeGuard<T> = (value: unknown) => value is T

export type { TypeGuard as default }

0 comments on commit 36aa5f8

Please sign in to comment.