-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-object.ts
31 lines (31 loc) · 970 Bytes
/
is-object.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
/**
* Determines whether the input is an object or not.
* @param value - input.
* @returns true if the input is an object, false otherwise.
* @remarks
* In JavaScript every thing is an object except primitive types:
* string - number - bigint - boolean - undefined - symbol - null
* MDN:
* https://developer.mozilla.org/en-US/docs/Glossary/Primitive
*
* In JavaScript, null is marked as one of the primitive values,
* because its behavior is seemingly primitive.
* However, when using the typeof operator, it returns "object".
* MDN:
* https://developer.mozilla.org/en-US/docs/Glossary/Null
*
* @example
* ```ts
* isObject({}) // true
* isObject(Function) // true
* isObject([]) // true
* isObject(null) // false
* isObject(1) // false
* isObject(true) // false
* ```
* @public
*/
export function isObject<T>(value: T): value is T & object {
const type = typeof value;
return value !== null && (type === 'object' || type === 'function');
}