-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
47 lines (46 loc) · 1.09 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
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { isObject } from '../isObject'
/**
* Checks if a value is `typeof object` and not `null`.
*
* @param value The value to check.
* @returns `true` if the check passes, `false` otherwise.
*
* @remarks
* This function will return `true` for an `array`. If you
* do not want this behavior, use {@link isObject} instead.
*
* @example
* #### Basic usage
* ```ts
* isObjectLoose({}) // true
* isObjectLoose([]) // true
* isPlainObject(null) // false
* ```
*
* #### Type narrowing
* ```ts
* const value: string | { foo: string }
*
* if (isObjectLoose(value)) {
* value // type: { foo: string }
* } else {
* value // type: string
* }
*
* // warning: arrays will pass
* const value: { bar: string } | string[] | null
*
* if (isObjectLoose(value)) {
* value // type: { bar: string } | string[]
* } else {
* value // type: null
* }
* ```
* @category Is
*/
export const isObjectLoose = <T>(
value: T
): value is T extends Function ? never : (T & object) => {
return !!value && typeof value === 'object'
}