Skip to content

Commit 254225d

Browse files
feat(guardDefined): guard value to not be undefined
1 parent 31fc781 commit 254225d

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Function.
2+
import { isDefined } from '../../is/lib/is-defined.func';
3+
// Type.
4+
import { Defined } from '../../type/defined.type';
5+
import { GuardDefined } from '../type/guard-defined.type';
6+
import { ResultCallback } from '../../type/result-callback.type';
7+
/**
8+
* Checks if a generic type `value` is NOT an `undefined` type and is NOT equal to `undefined`.
9+
* @param value A generic type `value` to check.
10+
* @param callback An optional `ResultCallback` function to handle result before returns.
11+
* @returns A `boolean` indicating whether or not the `value` is defined, if `undefined` then returns `never`.
12+
*/
13+
export const guardDefined: GuardDefined = <Type>(value: Type, callback?: ResultCallback): value is Defined<Type> =>
14+
isDefined(value, callback);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Defined } from '../../type/defined.type';
2+
import { ResultCallback } from '../../type/result-callback.type';
3+
export type GuardDefined = <Type>(value: Type, callback?: ResultCallback) => value is Defined<Type>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Defined<T> = T extends undefined ? never : T;

0 commit comments

Comments
 (0)