Function assertOrdinaryObject

  • Asserts that a value is an ordinary object.

    Unlike isOrdinaryObject, this function preserves the existing static type of the variable rather than narrowing it to a generic indexable structure. It accepts plain objects, custom-prototype objects, and ordinary class instances, while rejecting arrays, functions, primitives, and specialized built-in objects.

    Use this function when:

      - You expect a value to be an ordinary 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

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

    function run(opts: Options) {
    assertOrdinaryObject(opts, `'opts' is not an ordinary object.`);
    opts.value; // `opts` remains typed as `Options`.
    }

    if the value is not an ordinary object.