-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
55 lines (54 loc) · 1.12 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Checks if a value is `null` or `undefined`.
*
* @param value The value to check.
* @returns `true` if the check passes, `false` otherwise.
*
* @example
* #### Basic usage
* ```ts
* isNullish(null) // true
* isNullish(undefined) // true
* isNullish(0) // false
* ```
*
* #### Type narrowing
* ```ts
* const value: string | null | undefined
*
* if (isNullish(value)) {
* value // type: null | undefined
* } else {
* value // type: string
* }
*
* // or
* if (!isNullish(value)) {
* value // type: string
* } else {
* value // type: null | undefined
* }
* ```
*
* #### Paired with assert
* ```ts
* import { assert } from 'uft'
*
* const token: string | null | undefined
* assert(!isNullish(token), 'received nullish token')
* token // string
* ```
* @category Is
*/
export const isNullish = (value: unknown): value is null | undefined => {
return value == null
}
/**
* Checks if a value is not `null` or `undefined`.
* This is the inverse of {@link isNullish}.
*
* @category Is
*/
export const isNotNullish = <T>(value: T | null | undefined): value is T => {
return value != null
}