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