Objects.isObjectLiteral()
Syntax
function isObjectLiteral(value: unknown): value is ObjectLiteral
Checks if a value was created using object literal syntax (curly brackets), that is if it has a prototype other than Object
.
Parameters | |
value
|
The value to check. |
Example
import { Objects } from 'potence';
console.log(Objects.isObjectLiteral(new Date())); // -> false
console.log(Objects.isObjectLiteral({ a: 5 })); // -> true
Remarks
Note that there is no actual way to check how an object was created. Therefore,
this check should be considered an approximation rather than taken as absolute
truth. This function only checks whether the object’s prototype is Object
(that is, it has no other values in its prototype chain). As a result, calling
Objects.isObjectLiteral(new Object())
will actually return true
, even though
the object was not created using object literal syntax.
This function is most useful to ensure that a plain object was passed to your function, as opposed to, for example, a class instance.
Just like Objects.isObject()
, this
function serves as a type guard against an arbitrary
Record
type with values of type unknown
, allowing you to safely access any of the
value’s properties if the check succeeds.