Function assertObject

  • Asserts that a value is an object, not null, and not an array.

    Unlike isObject, 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 expect a value to be an object at runtime, **and**
    - You want to keep its compile-time type intact after validation.

    Type Parameters

    • T

    Parameters

    • value: T

      The value to validate.

    • OptionalerrorMsg: string

      Optional message used for the thrown TypeError.

    Returns asserts value is T & object

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

    function run(opts: Options = {}) {
    assertObject(opts, `'opts' is not an 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.