isPlainObject()
isPlainObject(
value):value is Record<string, any>
Checks if a value is a plain object (object literal or Object.create(null)).
๐ Why is this a Hidden Gem?
Distinguishes true dictionaries from arrays, null, or class instances.
note
Excludes arrays, Date, Map, Set, class instances, etc. Only {}, new Object(), and Object.create(null).
Parametersโ
value: unknownโ
The value to check.
Returns: value is Record<string, any>โ
true if the value is a plain object, false otherwise.
See Alsoโ
Sinceโ
1.0.0
Exampleโ
isPlainObject({}); // => true
isPlainObject({ a: 1 }); // => true
isPlainObject(new Object()); // => true
isPlainObject(Object.create(null)); // => true
isPlainObject([]); // => false
isPlainObject(new Date()); // => false
isPlainObject(new Map()); // => false
isPlainObject(new (class Foo {})()); // => false
isPlainObject(null); // => false
How it works?โ
Type guard that checks if a value is a plain object (object literal or Object.create(null)).
What Qualifiesโ
| Value | Result | Reason |
|---|---|---|
{} | true | Object literal |
{ a: 1 } | true | Object literal |
new Object() | true | Object constructor |
Object.create(null) | true | Null prototype |
[] | false | Array |
new Date() | false | Date instance |
new Map() | false | Map instance |
new MyClass() | false | Class instance |
null | false | Not an object |
isPlainObject vs isObjectโ
Use Casesโ
Identify configuration objects ๐โ
Ensure a value is a simple dictionary {} and not a complex object instance.
Perfect for merging configs or validating JSON payloads.
if (isPlainObject(config)) {
// Safe to merge or spread
const merged = { ...defaultConfig, ...config };
}
Deep clone decision logicโ
Determine if a value needs recursive cloning or can be copied by reference.
function deepClone(value: unknown): unknown {
if (!isPlainObject(value)) {
return value; // Primitives, arrays handled elsewhere, class instances kept as-is
}
const result: Record<string, unknown> = {};
for (const key in value) {
result[key] = deepClone(value[key]);
}
return result;
}
true
false