The value to check
true
if u
is a non-null object and not an array, false
otherwise. When true
, TypeScript narrows the type to UnknownRecord
.
const entries: unknown[] = [{ id: 1 }, ['tuple']];
const records = entries.filter(isRecord);
assert.deepStrictEqual(records, [{ id: 1 }]);
Type guard that checks if a value is a plain object (record) - a non-null object that is not an array.
This function is useful for identifying "plain" JavaScript objects (also called records or dictionaries) - objects that are typically used as key-value collections. It excludes arrays, functions, and special object types like Date, RegExp, etc., focusing on objects that can be safely treated as property collections.
Type Narrowing Behavior:
unknown
toUnknownRecord
(equivalent toRecord<PropertyKey, unknown>
)null
,undefined
, primitives, arrays, and functionstrue
for plain objects{}
, object literals, and objects created withObject.create()
false
for arrays, even though they are technically objectsImplementation: Uses
isNonNullObject()
to check for objects, thenArray.isArray()
to exclude arrays.