Objects.isObject()
Syntax
function isObject(value: unknown): value is ObjectLiteral
Checks if a value is an object (that is, not a primitive).
Parameters | |
value
|
The value to check. |
Example
import { Objects } from 'potence';
const obj: unknown = ...;
if (Objects.isObject(obj)) {
obj['a'] = 5; // no error
}
Remarks
This check returns false
on null
(even though JavaScript itself considers
null
to be an object), but returns true
on objects, functions, and arrays.
This function can be useful to check whether properties are supported on a
value. In other words, if this check succeeds, you can safely access properties
on the value using bracket syntax (e.g. value['myProp']
).
Note that, even though bracket syntax can be used if the check succeeds, there
is still no way to know if a property exists on the type or, if it does, what
type it is. Therefore, all properties will be typed as unknown
. To check
whether a specific property exists on a type, prefer either
Objects.hasProperty()
or
Objects.structure()
.
Compare primitive
and
isPrimitive()
.
For TypeScript Users
The function’s return type is value is ObjectLiteral
.
ObjectLiteral
is a helper type to mean “a
Record
of any valid key type and the specified value type” (by default: unknown
).
In other words: in this context, ObjectLiteral
only means that the object has
an arbitrary index signature, i.e. that you can access any key without a
TypeScript (or runtime) error, because ensuring that is the most likely reason
to use this function at all.
It does not mean that the value is literally an
object literal,
that is an object created via curly bracket syntax. If you’d like to check if an
object was created using curly bracket syntax, use
Objects.isObjectLiteral()
.