Function assertRecord

  • Asserts that a value is a non-null, non-array object that can be treated as a string-keyed record.

    Unlike isRecord, this function does not narrow the value to a generic indexable structure. Instead, it preserves the existing static type of the variable. This makes it ideal for validating option objects or interface-based inputs where all properties may be optional.

    Use this function when:

      - You need to reject `null`, primitives, or arrays at runtime.
    - You want to safely treat the value as a record, **without losing its compile-time shape**.

    Type Parameters

    • T

    Parameters

    • value: T

      The value to validate.

    • OptionalerrorMsg: string

      Optional message used for the thrown TypeError.

    Returns asserts value is T & Record<PropertyKey, unknown>

    interface Options { flag?: boolean; value?: number; }

    function run(opts: Options = {}) {
    assertPlainObject(opts, `'opts' is not a record object.`); // `opts` remains `Options`, not widened or reduced.
    opts.value; // Fully typed access remains available.
    }

    if the value is null, non-object, or an array.