Function assertNonNullObject

  • Asserts that a value is a non-null object, including arrays.

    Unlike isNonNullObject, this function preserves the existing static type of the variable while removing nullish, primitive, function, and class-constructor union members.

    This assertion accepts arrays, ordinary objects, class instances, boxed primitives, and specialized built-in objects such as Date, Map, and Set.

    Use this function when:

      - You expect a non-null object at runtime, including an array, **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 Exclude<
        T & object,
        ((...args: any[]) => any)
        | (new (...args: any[]) => any),
    >

    function process(value: string[] | (() => void) | undefined): void
    {
    assertNonNullObject(value);

    value.push('entry');
    // `value` is narrowed to `string[]`.
    }

    if the value is null, primitive, or callable.