Function assertObjectOrFunction

  • Asserts that a value is a non-null object or function.

    Unlike isObjectOrFunction, this function does not narrow the value to a generic object type. Instead, it preserves the existing static type of the variable while removing primitive and nullish union members.

    This assertion accepts all JavaScript reference values, including arrays, functions, class constructors, ordinary objects, and specialized built-in objects.

    Use this function when:

      - You expect a value to be an object or function 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

    function execute(value: Date | (() => void) | undefined): void
    {
    assertIsObjectOrFunction(value);

    // `value` is now `Date | (() => void)`.
    }

    if the value is null or a primitive value.