The value to check
true if u is a non-null, non-array object, false otherwise.
When true, TypeScript narrows the type to UnknownRecord.
const entries: readonly unknown[] = [
{ id: 1 },
[1, 2],
'str',
0,
null,
] as const;
const records = entries.filter(isRecord);
assert.deepStrictEqual(records, [{ id: 1 }]);
Type guard that checks if a value is a record — a non-null, non-array object — and narrows it to
UnknownRecord(=ReadonlyRecord<string, unknown>).This function is designed to be combined with hasKey to probe values of type
unknown(e.g. parsed JSON, caught errors, external data) in a type-safe way:if (isRecord(u) && hasKey(u, 'some-key')) { ... }.Type Narrowing Behavior:
unknowntoUnknownRecord(=ReadonlyRecord<string, unknown>)null,undefined, primitives, and functionsUnknownRecord; admitting them at runtime would be inconsistent with the narrowed typetruefor every other non-null object, including dictionary objects created withObject.create(null)and instances such asDate,RegExp,Map,Set,Error, and user-defined classesWhy non-plain objects are included: since every property of
UnknownRecordis typedunknown, reading properties from any object through this narrowing is type-safe, andhasKey(backed byObject.hasOwn) answers own-property questions correctly on any object (e.g.Mapentries are not own properties, sohasKeycorrectly returnsfalsefor them). Restricting the guard to plain objects would require prototype checks that misjudge cross-realm objects and would prevent probing useful instances such asErrorsubclasses.Implementation:
isNonNullObject(u) && !Array.isArray(u).