-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
55 lines (54 loc) · 1.01 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`.
*
* @param value The value to check.
* @returns `true` if the check passes, `false` otherwise.
*
* @example
* #### Basic usage
* ```ts
* isNull(null) // true
* isNull(undefined) // false
* isNull(0) // false
* ```
*
* #### Type narrowing
* ```ts
* const value: string | null
*
* if (isNull(value)) {
* value // type: null
* } else {
* value // type: string
* }
*
* // or
* if (!isNull(value)) {
* value // type: string
* } else {
* value // type: null
* }
* ```
*
* #### Paired with assert
* ```ts
* import { assert, isNull } from 'uft'
*
* const token: string | null
* assert(!isNull(token), 'received null token')
* token // type: string
* ```
* @category Is
*/
export const isNull = (value: unknown): value is null => {
return value === null
}
/**
* Checks if a value is not `null`.
* This is the inverse of {@link isNull}.
*
* @category Is
*/
export const isNotNull = <T>(value: T | null): value is T => {
return value !== null
}