ts-data-forge
    Preparing search index...

    Function isRecord

    • 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:

      • Narrows unknown to UnknownRecord (= ReadonlyRecord<string, unknown>)
      • Excludes null, undefined, primitives, and functions
      • Excludes arrays: arrays have no string index signature, so they are not assignable to UnknownRecord; admitting them at runtime would be inconsistent with the narrowed type
      • Returns true for every other non-null object, including dictionary objects created with Object.create(null) and instances such as Date, RegExp, Map, Set, Error, and user-defined classes

      Why non-plain objects are included: since every property of UnknownRecord is typed unknown, reading properties from any object through this narrowing is type-safe, and hasKey (backed by Object.hasOwn) answers own-property questions correctly on any object (e.g. Map entries are not own properties, so hasKey correctly returns false for them). Restricting the guard to plain objects would require prototype checks that misjudge cross-realm objects and would prevent probing useful instances such as Error subclasses.

      Implementation: isNonNullObject(u) && !Array.isArray(u).

      Parameters

      • u: unknown

        The value to check

      Returns u is UnknownRecord

      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 }]);
      • isNonNullObject - For checking any non-null object (includes arrays)
      • hasKey - For checking if a record has specific keys